为什么我会收到这个致命错误" Class' App \ Providers \ AuthServiceProvider'找不到"试图在Laravel 5.3中实现我的自定义用户提供程序?

时间:2017-01-25 19:52:11

标签: php laravel laravel-5 laravel-5.3 laravel-authorization

我绝对是 PHP Laravel 的新手,我来自Java。

我正在尝试按照本教程实施自定义用户提供程序

https://blog.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers/

我简要地说明了我的需求:我的 Laravel 应用程序只是一个前端应用程序,所有业务逻辑(包括用户身份验证)都由暴露REST Web服务的Java后端应用程序执行

拨打电话:

http://localhost:8080/Extranet/login

并将用户名和密码作为基本身份验证传递我获得了一个代表已登录用户的JSON响应:

{
  "userName": "Painkiller",
  "email": "painkiller@gmail.com",
  "enabled": true
}

因此,在我的Laravel应用程序中,我必须执行此调用,然后解析先前返回的JSON对象,以将经过身份验证的对象生成到前端应用程序会话中。

我认为之前的自定义用户提供商是最简洁,最自然的解决方案,但我发现了一些困难,我对如何在项目中做了很多疑问。

我正在使用 Larave 5.3 版本。

我已完成以下步骤(按照上一个教程并尝试根据我的需要进行调整):

1)第1阶段:注册您的自定义用户提供商:

我创建了服务提供商,其中我注册了mycustom身份验证用户提供程序(用于通过Web服务执行自定义登录的自定义组件)。

因此,在我的Laravel项目的 app 目录中,我创建了身份验证子目录,在这里我将 AuthServiceProvider 扩展为的ServiceProvider

<?php
namespace App\Authentication;

use Auth;
use App\Authentication\UserProvider;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider {
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Auth::provider('our_provider', function ($app, array $config) {
            return new UserProvider();
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

这只是注册您的用户提供者/驱动程序,使其可以通过our_provider键在config / auth.php文件中使用。

FIRST DOUBT :将鼠标放在:

use Auth;

结果未定义的类Auth ,为什么?在我看来,它没有在以前的代码中使用,可能是一个教程包含错误或我错过了什么?

然后我将此服务注册到我的 config / app.php 文件中:

这是此文件的提供商部分:

'providers' => [

    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Bus\BusServiceProvider::class,
    Illuminate\Cache\CacheServiceProvider::class,
    Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
    Illuminate\Cookie\CookieServiceProvider::class,
    Illuminate\Database\DatabaseServiceProvider::class,
    Illuminate\Encryption\EncryptionServiceProvider::class,
    Illuminate\Filesystem\FilesystemServiceProvider::class,
    Illuminate\Foundation\Providers\FoundationServiceProvider::class,
    Illuminate\Hashing\HashServiceProvider::class,
    Illuminate\Mail\MailServiceProvider::class,
    Illuminate\Notifications\NotificationServiceProvider::class,
    Illuminate\Pagination\PaginationServiceProvider::class,
    Illuminate\Pipeline\PipelineServiceProvider::class,
    Illuminate\Queue\QueueServiceProvider::class,
    Illuminate\Redis\RedisServiceProvider::class,
    Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    Illuminate\Session\SessionServiceProvider::class,
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,

    /*
     * Package Service Providers...
     */

    //

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    // App\Providers\BroadcastServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,
    App\Authentication\AuthServiceProvider::class,

],

如您所见,此数组包含我的 App \ Authentication \ AuthServiceProvider :: class,,代表之前的 ServiceProvider 。这是对的吗?

2:第2阶段:更新配置以使用新驱动程序:

我告诉我的Laravel应用程序使用以前的用户提供程序,所以我进入 config / auth.php 配置文件并设置我的提供程序:

'providers' => [
    'users' => [
        'driver' => 'our_provider',
    ],
],

为了完整起见,我发布了 config / auth.php 的全部内容,以便您查看是否还有其他错误:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    /*
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    */

    'providers' => [
        'users' => [
            'driver' => 'our_provider',
        ],


        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

3)第3阶段:创建用户类:

我认为这类似于模型对象,它将包含从我的Web服务接收的数据(如果用户可以登录系统)。

因此,如果我没有遗漏某些东西,它必须包含这样的结构(用户名,电子邮件,ebabled字段):

{
  "userName": "Painkiller",
  "email": "painkiller@gmail.com",
  "enabled": true
}

根据教程,此用户类必须实现Laravel Illuminate \ Contracts \ Auth \ Authenticatable接口

所以进入 / app / Authentication 类(包含前一个 AuthServiceProvicer 类的那个)我把这个用户类实现了可验证界面:

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\Authenticatable;

class User implements Authenticatable {
    /**
     * @return string
     */
    public function getAuthIdentifierName()
    {
        // Return the name of unique identifier for the user (e.g. "id")
    }

    /**
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        // Return the unique identifier for the user (e.g. their ID, 123)
    }

    /**
     * @return string
     */
    public function getAuthPassword()
    {
        // Returns the (hashed) password for the user
    }

    /**
     * @return string
     */
    public function getRememberToken()
    {
        // Return the token used for the "remember me" functionality
    }

    /**
     * @param  string $value
     * @return void
     */
    public function setRememberToken($value)
    {
        // Store a new token user for the "remember me" functionality
    }

    /**
     * @return string
     */
    public function getRememberTokenName()
    {
        // Return the name of the column / attribute used to store the "remember me" token
    }
}

DOUBT:教程示例只在这个类中加入了stubbded方法,我没有令牌,我认为我只想检索以前的用户名称,电子邮件,ebabled字段< / strong>即可。所以我认为这个课程是不完整的。我真的要做什么?创建这3个字段(表示我的Web服务返回的用户信息)和相关的getter方法?是或者我错过了什么?

4)第4阶段:创建UserProvider类:

在第1阶段,我有回调函数,它返回 App \ Authentication \ UserProvider 的实例,此类用于检索用户的实例应用\认证\用户即可。

此课程必须实现 Illuminate \ Contracts \ Auth \ UserProvider 界面。在我看来,必须实现的方法将包含检索我的用户信息的逻辑(因此在我的特定情况下,我必须进行我的Web服务调用)。

因此,在同一个应用/身份验证目录中,我已将此 UserProvider 实现照明\合同\ Auth \ UserProvider 界面,这是我的代码:

 <?php

 namespace App\Authentication;

 use Illuminate\Contracts\Auth\Authenticatable;
 use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
 use GuzzleHttp\Client;
 use function GuzzleHttp\json_encode;
 use function GuzzleHttp\json_decode;

class UserProvider implements IlluminateUserProvider
{
public function retrieveById($identifier)
{
    // TODO: Implement retrieveById() method.
}

public function retrieveByToken($identifier, $token)
{
    // TODO: Implement retrieveByToken() method.
}

public function updateRememberToken(Authenticatable $user, $token)
{
    // TODO: Implement updateRememberToken() method.
}

public function retrieveByCredentials(array $credentials)
{
    // TODO: Implement retrieveByCredentials() method.

    $client = new Client(); //GuzzleHttp\Client

    $response = $client->post('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                'Painkiller',
                'pswd'
            ]
        ]);


}

public function validateCredentials(Authenticatable $user, array $credentials)
{
    // TODO: Implement validateCredentials() method.
}

}

所以我认为我必须将此webservice调用放在此方法 retrieveByCredentials()中,这一个:

public function retrieveByCredentials(array $credentials)
{
    // TODO: Implement retrieveByCredentials() method.

    $client = new Client(); //GuzzleHttp\Client

    $response = $client->post('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                'Painkiller',
                'pswd'
            ]
        ]);


}

为了简洁起见,此时我已经对执行http请求的Web服务返回的现有用户的凭证进行了硬编码,因此,对于用户在登录表单中插入的任何凭据,将始终检索此用户(我想要做一个测试...然后我将与插入的凭证集成。

好的,这是我在本教程后理解的逻辑,它是正确的还是我遗漏了什么?

然后我按照 php artisan serve 的标准启动我的Laravel项目,但是我收到以下错误消息:

Andrea@Andrea-PC MINGW64 ~/Documents/Betrivius/WorkSpace/betriviusExtranet (master)
$ php artisan serve
PHP Fatal error:  Class 'App\Providers\AuthServiceProvider' not found in C:\Users\Andrea\Documents\Betrivius\WorkSpace\betriviusExtranet\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php on line 146


  [Symfony\Component\Debug\Exception\FatalErrorException]
  Class 'App\Providers\AuthServiceProvider' not found

为什么呢?怎么了?我错过了什么?我该如何解决这个问题?

0 个答案:

没有答案