[英]Logout issue with Laravel JWT-auth authentication
我正在使用jwt-auth在我的 API 中創建一個 RESTful auth 資源。 當客戶端應用程序調用登錄資源時,記錄用戶案例,您當前的令牌必須無效,因此會生成新的令牌。
但是,如果當前令牌被列入黑名單, TokenBlacklistedException
拋出TokenBlacklistedException
。
如何驗證令牌是否被列入黑名單? 或者如何正確實現用戶“注銷”? 我嘗試在 jwt-auth API 源上找到,但不存在getToken()->isBlacklisted()
或parseToken()->isBlacklisted()
或一些驗證器來實現它。
曾經令牌無效 parseToken() 拋出 TokenBlacklistedException,因此 isBlacklisted 方法是在使令牌無效之前驗證令牌是否有效的好方法。
信息:
下面的代碼驗證有效載荷是否無效,如果無效則拋出TokenBlacklistedException
:
if(
false === \Tymon\JWTAuth\Blacklist::has(
\Tymon\JWTAuth\Facades\JWTAuth::getPayload($token)
)
) {
\Tymon\JWTAuth\Facades\JWTAuth::parseToken()->invalidate();
}
如何驗證如下:
if(false ===\Tymon\JWTAuth\Facades\JWTAuth::parseToken()->isBlacklisted()) {
// invalidate...
}
您可以簡單地在客戶端注銷時銷毀會話並使后端的令牌無效,您不需要使用黑名單。
從技術上講,在客戶端銷毀令牌就足夠了,但是對於會話劫持,在后端使其無效也是一個好主意。
如果您正在失效,則需要在收到 Laravel 的響應后銷毀令牌。
JWTAuth::invalidate(JWTAuth::getToken());
然后在有角度的一側
function logout()
{
UserService.logout().$promise.then(function() {
$cookieStore.remove('userToken');
// redirect or whatever
});
}
處理 JWT 異常的一種方法是在 laravel 中設置一個EventServiceProvider
,這是我的樣子:
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider {
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
'tymon.jwt.valid' => [
'App\Events\JWTEvents@valid',
],
'tymon.jwt.user_not_found' => [
'App\Events\JWTEvents@notFound'
],
'tymon.jwt.invalid' => [
'App\Events\JWTEvents@invalid'
],
'tymon.jwt.expired' => [
'App\Events\JWTEvents@expired'
],
'tymon.jwt.absent' => [
'App\Events\JWTEvents@missing'
]
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
}
您將在 app.php 中注冊它。
然后我使用每個事件的方法實現 JWTEvents 類。
class JWTEvents extends Event {
// Other methods
public function invalid()
{
return response()->json(['error' => 'Token Invalid'], 401);
die();
}
}
需要注意的重要一點是,我們正在捕獲 JWT 異常並返回帶有特定狀態代碼的 json 響應。
在角度方面,我在 httpInterceptor 類中捕獲了這些 http 狀態代碼。
angular.module('ngApp')
.factory('httpInterceptor', function($q, $log, $cookieStore, $rootScope, Response) {
return {
request: function(config) {
// Where you add the token to each request
},
responseError: function(response) {
// Check if response code is 401 (or whatever)
if (response.status === 401) {
// Do something to log user out & redirect.
$rootScope.$broadcast('invalid.token');
}
}
}
});
這對我有用。
public function logout( Request $request ) {
$token = $request->header( 'Authorization' );
try {
JWTAuth::parseToken()->invalidate( $token );
return response()->json( [
'error' => false,
'message' => trans( 'auth.logged_out' )
] );
} catch ( TokenExpiredException $exception ) {
return response()->json( [
'error' => true,
'message' => trans( 'auth.token.expired' )
], 401 );
} catch ( TokenInvalidException $exception ) {
return response()->json( [
'error' => true,
'message' => trans( 'auth.token.invalid' )
], 401 );
} catch ( JWTException $exception ) {
return response()->json( [
'error' => true,
'message' => trans( 'auth.token.missing' )
], 500 );
}
}
據我了解,沒有人強調的一件事是用於刷新令牌的“jwt.refresh”(又名 RefreshTokenMiddleware)。
現在,如果任何想要執行注銷操作的人將控制器方法包裝在類似的路由中
Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function()...
肯定會在注銷響應中獲得一個新令牌,因此客戶端將能夠執行新請求。
希望這可以幫助澄清這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.