问题:Admin、User 分表管理后,验证Token是否过期,再是否成功认证?
产生原因:
- auth.php 配置文件中 defaults 配置的默认指向 user 用户所在数据表进行验证,对应 Token 先再默认 defaults 中实例化一次,代码如下:
$token = JWTAuth::getToken();
JWTAuth::getPayload($token);
解决方法:
- 1、创建一个中间件,AdminAuthMiddleware.php 代码如下:
public function handle($request, Closure $next)
{
$token = JWTAuth::getToken();
JWTAuth::getPayload($token);
return $next($request);
}
- 2、把中间件注册进 app/Http/Kernel.php 内核中:
protected $routeMiddleware = [
//Check-Admin-Token
'auth.admin' => \App\Http\Middleware\AdminAuthMiddleware::class,
];
protected $middlewarePriority = [
//Check-Admin-Token
\App\Http\Middleware\AdminAuthMiddleware::class,
];
$api->group(['middleware' => ['auth.admin:admin_api', 'checkPermission']], function ($api) {
//Refresh Staff-Token
$api->patch('/authentications/token', 'AuthorizationsController@update')->name('api.authorizations.Update');
});
}
网友评论