我是不熟悉角度8的人。我有一个拦截器:
export class HttpRequestInterceptor implements HttpInterceptor {
private apiAddress = 'http://localhost:1080';
private refreshTokenIsInProgress = false;
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
let cloneReq;
//...
// some codes like adding access token to header
//...
cloneReq = request.clone();
return next.handle(cloneReq)
.pipe(
catchError((error: any) => {
if(error.status==401)//means token expired
{
//Here i need help
//Get NEW Token And Replace With previous And Resend Current Request
}
return of(error);
})
)
;
}
}
export const httpInterceptorProviders = [
{ provide: HTTP_INTERCEPTORS, useClass: HttpRequestInterceptor, multi: true },
];
我的请求就像:
return this.http.get(url).subscribe();
正如我提到的,如果请求返回401(未经授权),我需要从服务器获取新令牌(JWT),将其替换为先前的令牌,然后重新发送CURRENT请求;获取新令牌没有问题!问题是替换新令牌并重新发送请求;
我搜索和搜索了很多表格,但是...我以为我可以使用retry()和retryWhen(())达到目标,但它们无法更改请求参数。谢谢大家朋友
尚未测试代码,但是您可以尝试将请求包装到更高阶的函数中并访问请求对象,也可以递归调用函数,直到满足条件为止。
const repeatRequest=(cloneReq)=>next.handle(cloneReq)
.pipe(
catchError((error: any) => {
if(error.status==401)//means token expired
req=........ // modify your request here
return repeatRequest(req)
return of(error);
})
)
cloneReq = request.clone();
return repeatRequest(cloneReq)