简体   繁体   English

Laravel JWT 令牌总是被列入黑名单

[英]Laravel JWT Token Always Blacklisted

I am using the tymondesigns/jwt-auth package for my app.我正在为我的应用程序使用 tymondesigns/jwt-auth 包。 I use customClaims to make my token.我使用 customClaims 来制作我的令牌。 Here is the code for login :这是登录的代码:

$token_data = [
        'iss' => new Issuer('AreteHCM'),
        'iat' => new IssuedAt(Carbon::now()) ,
        'exp' => new Expiration(Carbon::now()->addDays(1)),
        'nbf' => new NotBefore(Carbon::now()),
        'sub' => new Subject('AreteHCMS'),
        'jti' => new JwtId('AreteHCM'),
        'user_data' => $user->user,
        'menu_access' => $menu_access,
        'login_time' => Carbon::now(),
];
    
$customClaims = JWTFactory::customClaims($token_data);
$payload = JWTFactory::make($customClaims);
$token = JWTAuth::encode($payload)->get();

For Logout, I invalidate the token, so the token can not be used anymore after the user logout.对于注销,我使令牌无效,因此在用户注销后无法再使用令牌。

JWTAuth::invalidate(JWTAuth::getToken());

I'm creating API (Backend) and the front end team using Angular.我正在使用 Angular 创建 API(后端)和前端团队。 Eveythings went smooth, until the user logout and try to login again.一切都很顺利,直到用户注销并再次尝试登录。 After login, the user get the new account, but when he/she wants to access my middleware always rejects the token, it says that the token is blacklisted.登录后,用户获得新帐户,但是当他/她想访问我的中间件时总是拒绝令牌,它说令牌被列入黑名单。

Here is my middleware :这是我的中间件:

$token = JWTAuth::getToken();
$data = JWTAuth::getPayload($token)->toArray();

It always shows error :它总是显示错误:

The token has been blacklisted in file C:\xampp\htdocs\aretehcm\vendor\tymon\jwt-auth\src\Manager.php on line 109

What I want to approach is :我想接近的是:

  • User login get new token to access the API (every API request requires header auth Bearer token)用户登录获取新令牌以访问 API(每个 API 请求都需要 header auth Bearer 令牌)
  • User logout will invalidate the token, so the token can not be used anymore to access the API用户注销将使令牌失效,因此无法再使用令牌访问 API
  • User login will get a new token so he/she can access the API用户登录将获得一个新令牌,以便他/她可以访问 API

Is there any misconception from me about the JWT-API architecture ?我对 JWT-API 架构有什么误解吗? Thank you in advance for your replies and answers.预先感谢您的回复和回答。

一个快速的谷歌搜索让我找到了这个解决方案: https : //github.com/tymondesigns/jwt-auth/issues/983#issuecomment-275884324

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM