May*_*onn 8 php authentication jwt laravel angularjs
我使用jwt-auth在我的API中创建RESTful auth资源.当客户端应用程序调用登录资源时,将记录案例用户,您的当前令牌必须无效,因此会生成新令牌.
但是当前令牌被列入黑名单的情况下TokenBlacklistedException
被抛出.
如何验证令牌是否被列入黑名单?或者如何纠正实施用户"注销"?我尝试在jwt-auth API源上找到但不存在一个getToken()->isBlacklisted()
或一个parseToken()->isBlacklisted()
验证器来实现它.
永久令牌无效parseToken()抛出TokenBlacklistedException,因此isBlacklisted方法是在令牌无效之前验证令牌是否有效的好方法.
信息:
波纹管代码验证有效负载是否无效,抛出TokenBlacklistedException
if无效:
if(
false === \Tymon\JWTAuth\Blacklist::has(
\Tymon\JWTAuth\Facades\JWTAuth::getPayload($token)
)
) {
\Tymon\JWTAuth\Facades\JWTAuth::parseToken()->invalidate();
}
Run Code Online (Sandbox Code Playgroud)
如何验证如下:
if(false ===\Tymon\JWTAuth\Facades\JWTAuth::parseToken()->isBlacklisted()) {
// invalidate...
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*own 14
您可以简单地在客户端销毁会话并在后端使令牌无效时,您不需要使用黑名单.
从技术上来说,破坏客户端的令牌就足够了,但是对于会话劫持,在后端使其无效也是一个好主意.
如果您无效,则需要在收到Laravel的回复后销毁令牌.
JWTAuth::invalidate(JWTAuth::getToken())):
Run Code Online (Sandbox Code Playgroud)
然后在角度方面
function logout()
{
UserService.logout().$promise.then(function() {
$cookieStore.remove('userToken');
// redirect or whatever
});
}
Run Code Online (Sandbox Code Playgroud)
处理JWT异常的一种方法是设置一个EventServiceProvider
laravel,这是我的样子:
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);
//
}
}
Run Code Online (Sandbox Code Playgroud)
您将在app.php中注册.
然后我用每个事件的方法实现JWTEvents类.
class JWTEvents extends Event {
// Other methods
public function invalid()
{
return response()->json(['error' => 'Token Invalid'], 401);
die();
}
}
Run Code Online (Sandbox Code Playgroud)
需要注意的重要一点是,我们正在捕获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');
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
这对我有用。
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 );
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16780 次 |
最近记录: |