无痛刷新jwt-token多[guard]版本
<?php
namespace App\Http\Middleware;
use Auth;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class RefreshToken extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*
* @return mixed
*/
public function handle($request, Closure $next, $role='api')
{
// 检查此次请求中是否带有 token,如果没有则抛出异常。
$this->checkForToken($request);
// 判断token是否在有效期内
try {
if (auth($role)->payload()) {
app('auth')->shouldUse($role);
return $next($request);
}
} catch (JWTException $exception) {
try{
$token = auth($role)->refresh();
// 使用一次性登录以保证此次请求的成功
auth($role)->onceUsingId(
auth($role)->payload()->get('sub')
);
//更新请求中的token
$request->headers->set('Authorization','Bearer '.$token);
} catch(JWTException $exception) {
// 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。
throw new UnauthorizedHttpException('jwt-auth', $exception->getMessage());
}
}
// 在响应头中返回新的 token
$token = $token ?: auth($role)->refresh();
$response = $next($request);
$response->headers->set('Authorization', 'Bearer '.$token);
return $response;
}
}
使用方式:route 中间件中添加'refresh.token:role'
特别注意:
1、当使用 refresh.token:role 中间件之后遇到用户授权 authorize 失败。导致非默认 guard 的角色操作授权失败。
因为 app.auth 即 AuthManager 里面使用的 guard 是 auth 中配置的默认 guard。
AuthServiceProvider.php:#
protected function registerAuthenticator()
{
$this->app->singleton('auth', function ($app) {
$app['auth.loaded'] = true;
return new AuthManager($app);
});
$this->app->singleton('auth.driver', function ($app) {
return $app['auth']->guard();
});
}
AuthManager.php:#
/**
* Get the default authentication driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->app['config']['auth.defaults.guard'];
}
这将导致我们的 policy 授权方法无法注入正确的 user 导致授权失败。
所以在 RrefreshToken 中间件中获取到正确的 token 时候,我们需要修改 AuthManager 的 guard。避免用户操作无法正确授权。
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 4年前 自动加精
推荐文章: