laravel 用户登录自定义登录验证

laravel 的用户登录是在配置文件中的auth.php当中,

用户登录走的是

// web端用的
Auth::attempt()// 也就是对应的sessiongurad的attempt()方法
其中涉及到了UserProvider
// api用的
Auth::user()// 也就是对应的Tokengurad的user()方法
其中涉及到了UserProvider

web端api之所以不一样是因为他们的验证方式不同,api一般都是用token web用的是session存储

 SessionGurd中的attempt

laravel 用户登录自定义登录验证_第1张图片

TokenGuard中的user方法

laravel 用户登录自定义登录验证_第2张图片

 [
        '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",
虽说支持"session", "token",但是我们也可以自己写guard,参考:https://learnku.com/articles/13434/the-laravel-framework-extends-auth-authentication-to-implement-custom-driver-guard
    |不同的guard类型的配置
    */

    'guards' => [
        'web' => [
            'driver' => 'session',// 标志是sessionGuard,不懂请参考AuthManager类中的 protected function resolve($name)、   public function createSessionDriver($name, $config)
            'provider' => 'users',// 数据提供者配置,不明白同样参考AuthManager
        ],

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

    /*
    |--------------------------------------------------------------------------
    | User Providers  是用来验证用户的token登录和session登录的,可以参考EloquentUserProvider类,整个流程 AuthManager-》createSessionDriver中的createUserProvider。如果需要更改用户的登录判断则在EloquentUserProvider种修改
    |--------------------------------------------------------------------------
    |
    |对应'guards'数组中的provider
    */

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

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

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    */

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

];
Authentication Guards、User Providers均可以自定义,在AuthManager中的extend方法,和provider方法
自定义Guards参考https://learnku.com/articles/13434/the-laravel-framework-extends-auth-authentication-to-implement-custom-driver-guard
自定义Providers参考https://www.cnblogs.com/fenle/p/5650882.html

你可能感兴趣的:(php)