我正在使用角度拦截器在标头中添加授权令牌。它可以正常工作,直到令牌到期。 laravel在令牌到期后发送token_expired错误。我需要以角度捕获该错误并发送请求以发出新令牌。发出新令牌后,我需要重新发送失败的请求。
角度代码中的拦截器
>>> p = psutil.Process()
>>> for i in range(10):
... p.io_counters()
...
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
验证JWT中间件
import {Injectable, NgModule} from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor, HTTP_INTERCEPTORS
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import {LoginService} from './login.service';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(public auth: LoginService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});
return next.handle(request);
}
}
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true }
]
})
export class InterceptorModule { }
laravel中的路线
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Facades\JWTAuth;
class VerifyJWTToken
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
try{
$user = JWTAuth::toUser($request->bearerToken());
}catch (TokenExpiredException $e){
//JWTAuth::setToken(JWTAuth::refresh());
return response()->json(['token_expired']);
}
catch (\Exception $e) {
return response()->json(['error']);
}
return $next($request);
}
}
有什么方法可以捕获来自服务器的所有响应并检查token_expired错误并重新发出令牌。
答案 0 :(得分:0)
抓住HttpErrorResponse
错误:
...
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
this.router = this.injector.get(Router);
return next.handle(request).do(
(event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
},
(err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
// you can redirect to login page
this.router.navigate(['/login']);
// or do some request...
} else {
let title = 'Server error';
...
}
}
}
);
}
如果用户闲置x分钟,那将是正确的。但如果 他一直在线,然后他必须重新授权
向您的auth.service
添加令牌续订功能,该功能会在一段时间后调用。写好here