【问题标题】:laravel JWT token can be used just once and it gets invalid token on second trylaravel JWT 令牌只能使用一次,第二次尝试获取无效令牌
【发布时间】:2017-10-22 23:54:06
【问题描述】:

我正在使用 JWT 令牌,它在前一段时间运行良好。但是现在,每当我使用令牌时,我都会在第一次尝试中得到我想要的结果,然后我第二次检查它(2 分钟后),我得到了无效的令牌: 这是我的验证码:

  $credentials = $request->only('email', 'password');

    try {
        // verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // if no errors are encountered we can return a JWT
    return response()->json(compact('token'));

这是我的 web.php 文件

Route::group(['prefix' => 'api/v1','middleware' => ['cors']], function(){

Route::resource('authenticate', 'AuthenticateController');
Route::post('authenticate', 'AuthenticateController@authenticate');
Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function() {
    Route::resource('books', 'BooksController', ['except'=>'store', 'update']);
});

});

【问题讨论】:

  • 您是否正在存储 token created ?在您的本地存储或 cookie 中
  • 我不这么认为,我该如何检查?
  • 您是否更改了config/jwt.php 中的令牌生命周期?
  • 你是说ttl?默认值为60
  • @DrStein 是的。 ttl 是生命周期。默认 60 分钟。

标签: php laravel jwt


【解决方案1】:

当您使用刷新令牌(jwt.refresh 中间件)时,这是预期的行为。

https://github.com/tymondesigns/jwt-auth/wiki/Authentication

此中间件将再次尝试从请求中解析令牌,然后刷新令牌(从而使旧令牌无效)并将其作为下一个响应的一部分返回。这实质上产生了单次使用的令牌流,如果令牌被泄露,它会减少攻击窗口,因为它仅对单个请求有效。

如果您不想使用刷新令牌,则可以删除该中间件。如果您确实想使用刷新令牌,则需要更新用于在每个请求上进行身份验证的令牌。

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 2016-10-20
    • 1970-01-01
    • 2021-10-20
    • 2016-07-27
    • 2021-04-13
    • 2019-05-07
    • 2019-03-19
    • 2021-04-05
    相关资源
    最近更新 更多