所以我有我的身份验证服务 AuthService ,基本上有两种方法,一种是从服务器获取新令牌,给定用户名和密码,另一种是检索当前存储的令牌并在必要时刷新令牌 . 两者显然都依赖于HttpClient . 也就是说, AuthService 依赖于HttpClient . 让我们记住这一点 .
另一个"service"是一个HttpInterceptor,我希望拦截除 AuthService 之外的所有传出请求以添加Authorization标头(它现在变脏了) . 为了弥补这个 Headers ,我们需要一个令牌,我们从 AuthService 获得 . 也就是说, AuthInterceptor (我的拦截器的名称)依赖于 AuthService . 据我所知,HttpClient依赖于所有HTTP_INTERCEPTORS .
所以场景如下:
关于如何打破这个圈子的任何想法或建议?有没有办法让 AuthService 的HttpClient独立于 AuthInterceptor ?或者这是一个坏主意? (另外第三个函数将添加到 AuthService 以便将用户注销,其请求将被截获并且还将Authorization标头添加到其中)
到目前为止,我发现了一个类似的issue但是变通方法建议没有't solve my problem, now I get infinite recursion during the bootstrapping process before any requests are sent. I'处理登录和令牌刷新请求的情况被截获以避免this所以据我所知这不是问题所在 . 这是一个plunk,其中包含我的代码概述 .
摘自摘录:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
private auth: AuthService;
constructor(inj: Injector) {
this.auth = inj.get(AuthService);
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Ignore if login or refresh request
if (req.url.includes('login')) {
return next.handle(req);
}
console.log('Intercepting request...');
return this.auth.getToken().map(token => {
const authHeader = 'Bearer ' + token;
const authReq = req.clone({setHeaders: {Authorization: authHeader}});
return authReq;
}).concatMap(newReq => next.handle(newReq));
}
}
3 回答
Update 08/02/2018 - angular 5.2.3
只是对此的更新:这是在角度5.2.3中修复的
https://github.com/angular/angular/blob/master/CHANGELOG.md#bug-fixes-2
因此,您可以在HttpInterceptors中直接注入依赖于HttpClient的服务
尝试使用超时设置
this.auth
:您已链接到的错误报告已更新,并提供了替代解决方法(在拦截函数中检索AuthService /而不是在构造函数中设置它):https://github.com/angular/angular/issues/18224#issuecomment-316957213
我使用Angular 6.1.10遇到了相同或类似的问题 . 我只是在需要注入HttpInterceptor的服务中自己实例化HttpClient:
这打破了我的无限循环问题 .