我使用 lumen(5.2) 框架和 jwt(1.0)。
我得到一个令牌,但我无法刷新它,因为系统告诉我“令牌已被列入黑名单”。不知道怎么解决的。请你帮助我好吗。
原谅我,我的英文不是很好,表达方式可能会有些差异。
路线
$app->group(['prefix' => 'auth', 'namespace' => '\App\Http\Controllers'], function () use ($app) {
$app->post('/signin', 'AuthController@signin');
$app->put('/refresh', ['middleware' => ['before' => 'jwt.auth', 'after' => 'jwt.refresh'], 'uses' => 'AuthController@refresh']);
});
登入
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();
});
}