2017-09-22 200 views
0

我试图在使用Tymon的JWTAuth的Laravel 5.5中实现基于令牌的身份验证。我遵循GitHub Documentation的库,并使用以下身份验证流程。这里是我的登录路由的认证部分:Laravel Tymon JWT验证:在验证之前检查杰出的有效令牌吗?

try { 

    // attempt to verify the credentials and create a token for the user 
    if (!$token = JWTAuth::attempt($credentials)) { 
     return response()->json(['success' => false, 'error' => 'Invalid Credentials. Please make sure you entered the right information and you have verified your email address.'], 401); 
    } 

} 

catch (JWTException $e) { 
    // something went wrong whilst attempting to encode the token 
    return response()->json(['success' => false, 'error' => 'could_not_create_token'], 500); 
} 

// all good so return the token 
return response()->json(['success' => true, 'data'=> [ 'token' => $token ]]); 

而且这里的路线:

Route::group([ 

    'middleware' => ['jwt.auth', 'jwt.refresh'], 

    ], 

    function() { 

     // Routes requiring authentication 
     Route::get('/logout', 'Auth\[email protected]'); 
     Route::get('/protected', function() { 
      return 'This is a protected page. You must be logged in to see it.'; 
    }); 

}); 

所以你可以看到我使用的jwt.auth和jwt.refresh中间件。现在,一切看起来都按预期工作,我可以使用令牌来验证用户身份。每个令牌具有一次使用的生命周期,并且在每次请求(刷新流)后我都会提供另一个有效令牌。

然而,我的问题是,如果我有一个还没有被使用呢,然后我从报头中取出,并击中有效凭证/登录路由的用户的有效令牌,我发出另一个有效的令牌。所以现在我有两个有效的令牌可以用来验证用户,因为我的/登录路由不会使先前发出的令牌无效。

有谁知道一种方法来检查用户是否有一个未解决的有效令牌,以便它可以失效,如果用户从其他地方登录?

回答

0

我会在做一些研究后回答我自己的问题。根据我的理解,除非明确列入黑名单,否则JWT令牌是有效的。只要服务器认识到它自己创建了令牌,那么它就可以使用密钥解密令牌并假定它是有效的。这就是为什么令牌寿命如此重要。所以如果你想使发行的令牌无效,你必须等待到期或黑名单。