我正在尝试获取刷新令牌后重做请求。 我昨天花了90%的时间尝试对他人有用的不同方法,但是我没有发现任何有效的方法。 我有3个拦截器。 第一个处理请求设置, 第二个处理令牌注入,然后处理任何有效的响应, 第三者处理任何错误。
我已经尝试在refreshToken调用上使用switchMap,但是它甚至从未进行过调用。这样,它会进行第一个调用,并返回令牌已过期的信息。我进行刷新呼叫并设置新令牌,但它拒绝重做该呼叫。我想念什么吗?
拦截器1:
export class APIInterceptor implements HttpInterceptor {
constructor() { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const clonedReq = req.clone({ url: req.url, responseType: 'json' });
return next.handle(clonedReq);
}
}
拦截器2:
export class JwtInterceptor extends BaseService implements HttpInterceptor {
constructor(private injector: Injector, private notificationService: NotificationService) {
super();
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authService = this.injector.get(AuthenticationService);
const globalService = this.injector.get(GlobalService);
const authReq = req.clone({
headers: req.headers.set('Authorization', this.getAuthToken(req, authService))
.append('Access-Control-Allow-Origin', '*')
});
return next.handle(authReq).map((event: any) => {
globalService.handleLayoutSetup();
if (event instanceof HttpResponse) {
const jsonResponse = <ApiResponse>event.body;
if (jsonResponse.status === 200) {
const body = this.objectToBlob(jsonResponse.data);
const headers = event.headers;
const status = event.status;
const statusText = event.statusText;
const url = event.url;
const response = new HttpResponse({ body, headers, status, statusText, url });
return response;
} else
this.notificationService.htmlNotificationFromErrors(jsonResponse.errors);
} else
return event;
});
}
private getAuthToken(req: HttpRequest<any>, authService: AuthenticationService) {
const currentUser = authService.getCurrentUser();
if (currentUser)
return 'Bearer ' + currentUser.accessToken.token;
else
return '';
}
}
拦截器3:
export class ValidationInterceptor extends BaseService implements HttpInterceptor {
private authService: AuthenticationService;
private authClient: AuthClient;
constructor(private notificationService: NotificationService, private injector: Injector) {
super();
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.authService = this.injector.get(AuthenticationService);
const globalService = this.injector.get(GlobalService);
const signalrService = this.injector.get(SignalRService);
this.authClient = this.injector.get(AuthClient);
return next.handle(req).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
}
}, (errResponse: any) => {
if (errResponse instanceof HttpErrorResponse) {
if (errResponse.status === 401 && errResponse.headers.has('Token-Expired')) {
return this.authClient.getRefreshToken('0d3452a923d7eb05331ae0e41d0d5e3a')
.subscribe((res: string) => {
this.authService.updateTokenWithRefreshToken(res);
req = this.addAuthHeader(req);
return next.handle(req);
});
} else if (errResponse.status === 0)
this.notificationService.failure('Error', 'Server is offline');
else {
.. handle errors
}
}
});
}
addAuthHeader(request) {
const authHeader = this.authService.getToken();
if (authHeader) {
return request.clone({
setHeaders: {
'Authorization': authHeader
}
});
}
return request;
}
}