Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel JWT令牌只能使用一次,第二次尝试时它会得到无效的令牌_Php_Laravel_Jwt - Fatal编程技术网

Php laravel JWT令牌只能使用一次,第二次尝试时它会得到无效的令牌

Php laravel JWT令牌只能使用一次,第二次尝试时它会得到无效的令牌,php,laravel,jwt,Php,Laravel,Jwt,我正在使用JWT令牌,它在不久前工作得很好。但现在,每当我使用令牌时,我在第一次尝试中得到我想要的结果,并在第二次检查它(2分钟后),我得到无效令牌: 这是我的身份验证代码: $credentials = $request->only('email', 'password'); try { // verify the credentials and create a token for the user if (! $token = JWTAu

我正在使用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']);
});

}))

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

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


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

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

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


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

是否存储创建的
令牌?在您的本地存储或Cookies中我不这么认为,我如何检查?您是否在
config/jwt.php
中更改了令牌生存期?您是指ttl?默认值为60value@DrStein对ttl是生命周期。默认60分钟。您是否正在存储创建的
令牌?在您的本地存储或Cookies中我不这么认为,我如何检查?您是否在
config/jwt.php
中更改了令牌生存期?您是指ttl?默认值为60value@DrStein对ttl是生命周期。默认为60分钟。