我正在尝试使用刷新令牌在过期后获得新的JWT令牌,还重新发送了由于我的JWT过期而失败的请求,我可以使用以下代码成功地做到这一点。
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (request.url.indexOf('Auth/refresh') !== -1) {
let headers = { Authorization: this.authService.getRefreshToken() };
request = request.clone({
setHeaders: headers
});
}
const customReq = request.clone({
// headers: request.headers.set('app-language', 'it')
});
return next
.handle(customReq)
.do((ev: HttpEvent<any>) => {
if (ev instanceof HttpResponse) {
})
.catch(response => {
if (response instanceof HttpErrorResponse) {
if (response.error.startsWith("ExpriedJWT") ){
console.log("Token Expired");
if (!this.refreshRequestSent) {
this.authService.refreshToken().subscribe((data: any) => {
console.log("Token Renewd");
this.sendFailedRequests(next);
this.refreshRequestSent = false;
});
this.refreshRequestSent = true;
}
this.failedRequests.push(customReq);
}
}
return observableThrowError(response);
});
}
private sendFailedRequests(next: HttpHandler) {
for (let i = 0; i < this.failedRequests.length; i++) {
next.handle(this.failedRequests[i]);
}
this.failedRequests = [];
}
问题在于,已经收到401并且我已发送请求的订阅者不再监听,即使我得到了响应。这对我没有任何用处。回到我的问题,在这种情况下,我如何让那些订阅者继续收听或工作