如何使用流明刷新jwt中的令牌?
问题描述:
我使用流明(5.2)框架和jwt(1.0)。如何使用流明刷新jwt中的令牌?
我得到一个令牌但我无法刷新它,因为系统告诉我“该令牌已被列入黑名单”。我不知道如何解决它。你可以帮我吗。
原谅我,我的英文不是很好,可能会有一些表达上的差异。
路线
$app->group(['prefix' => 'auth', 'namespace' => '\App\Http\Controllers'], function() use ($app) {
$app->post('/signin', '[email protected]');
$app->put('/refresh', ['middleware' => ['before' => 'jwt.auth', 'after' => 'jwt.refresh'], 'uses' => '[email protected]']);
});
登录
public function signin(Request $request)
{
$this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required'
]);
try {
if ($token = $this->jwt->attempt($request->only(['email', 'password']))) {
return $this->json([
'token' => $token
]);
}
return $this->json([], 403, $this->_lang['signin_incorrect']);
} catch (JWTException $e) {
return $this->json([], 500, $e->getMessage());
}
}
刷新
public function refresh()
{
try {
$this->jwt->setToken($this->jwt->getToken());
if($this->jwt->invalidate()) {
return $this->json([
'token' => $this->jwt->refresh()
]);
}
return $this->json([], 403, $this->_lang['token_incorrect']);
} catch (JWTException $e) {
return $this->json([], 500, $e->getMessage());
}
}
验证服务提供商
public function boot()
{
// Here you may define how you wish users to be authenticated for your Lumen
// application. The callback which receives the incoming request instance
// should return either a User instance or null. You're free to obtain
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function ($request)
{
return \App\Models\User::where('email', $request->input('email'))->first();
});
}
答
我已经解决了这个问题。
先我删除jwt.refresh
中间件。然后我使用JWT MANAGER刷新我的令牌。
这是现在的代码
$app->group(['prefix' => 'auth', 'namespace' => '\App\Http\Controllers'], function() use ($app) {
$app->post('/signin', '[email protected]');
$app->put('/refresh', '[email protected]');
});
控制器
return $this->json([
'token' => $this->manager->refresh($this->jwt->getToken())->get()
]);
我可以知道你使用的是什么版本的智威汤逊的?我有同样的问题,并做了这个,但它仍然存在。我正在使用'1.0.0-beta.3' – basagabi