在 laravel文档中,自定义登录校验在 https://learnku.com/docs/laravel/5.8/authentication/3906#b5c52b ,自定义看守器
1. 在laravel自带的看守器认证上进行扩展
即在任意服务提供者中,进行 Auth::extend()。如 文档中所示:
<?php
namespace App\Providers;
use App\Services\Auth\JwtGuard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* 注册任意应用认证/授权服务。
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Auth::extend('jwt', function ($app, $name, array $config) {
// 返回一个 Illuminate\Contracts\Auth\Guard 实例...
return new JwtGuard(Auth::createUserProvider($config['provider']));
});
}
}
Auth::extend('jwt') , jwt 是要自定义实现 guard 的 类
闭包要返回这个类 ( 实现 guard 的自定义的类 ) 的实例
2.实现自定义的 guard
实现时只要覆盖实现 user() 方法即可,其他需要实现的方法在 GuardHelper 的 trait 中已实现
3.Auth.php 配置自定义的 guard
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
4.修改一下登录不成功的重定向的路径
https://learnku.com/docs/laravel/5.8/authentication/3906#fdb792
即 app/Http/Middleware/Authenticate.php 中的 redirectTo 方法