HttpInterceptor-> Service-> HttpClient循环依赖 [英] HttpInterceptor->Service->HttpClient Cyclic Dependency

查看:106
本文介绍了HttpInterceptor-> Service-> HttpClient循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有我的身份验证服务 AuthService ,该服务基本上有两种方法,一种是从服务器获取新令牌(给定用户名和密码),另一种是检索当前存储的令牌.并在此过程中在必要时刷新令牌. 两者显然都依赖于 HttpClient .也就是说, AuthService HttpClient 具有依赖性.让我们记住这一点.

So I have my authentication service, AuthService, that basically has two methods, one that gets a new token from the server, given a username and a password, and one that retrieves the current stored token and refreshes the tokens during the process when necessary. Both obviously rely on HttpClient. That is, AuthService has a dependency on HttpClient. Let's keep that in mind.

另一个服务"是一个 HttpInterceptor ,我要拦截除 AuthService 的所有传出请求以添加授权标头之外的所有传出请求(现在变得很脏).为了组成该标头,我们需要一个令牌,该令牌是从 AuthService 获得的.也就是说, AuthInterceptor (我的拦截器的名称)具有对 AuthService 的依赖性.据我所知, HttpClient 对所有 HTTP_INTERCEPTORS 都有依赖性.

Another "service" is an HttpInterceptor that I want to intercept all outgoing requests other than those made by AuthService to add the Authorization header (it's getting dirty now). And to make up that header, we need a token, which we get from AuthService. That is, AuthInterceptor (the name of my interceptor) has a dependency on AuthService. And as far as I know, HttpClient has a dependency on all HTTP_INTERCEPTORS.

因此,情况如下:

关于如何打破这一圈子的任何想法或建议? 有什么方法可以使 AuthService HttpClient 独立于 AuthInterceptor ?还是一个坏主意? (将向 AuthService 添加另一个第三功能,以注销用户,该用户的请求将被拦截,并向其中添加Authorization标头)

Any ideas or suggestions on how to break that circle? Is there any way to make AuthService's HttpClient independent of AuthInterceptor? Or is it a bad idea? (Another third function will be added to AuthService for logging the user out, whose requests shall be intercepted and have the Authorization header added to them as well)

到目前为止,我发现了类似的问题,但是建议的解决方法没有解决了我的问题,现在在发送任何请求之前,我在引导过程中得到了无限递归.我已经处理了登录和令牌刷新请求被拦截的情况,以避免据我所知,这不是这里的问题. 这是一个插件,其中包含我的代码概述.

So far I found a similar issue but the workaround suggested there doesn't solve my problem, now I get infinite recursion during the bootstrapping process before any requests are sent. I've handled the case of login and token refresh requests being intercepted to avoid this so this is not the issue here as far as I know. Here's a plunk with an overview of my code.

摘录摘录:

@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));
  }
}

推荐答案

更新08/02/2018-角5.2.3

对此进行了更新:在角度5.2.3中已修复

Just an update on this: this was fixed in angular 5.2.3

https://github.com/angular/angular/blob/master/CHANGELOG.md#bug-fixes-2

因此您可以在HttpInterceptors中直接注入依赖于HttpClient的服务

So you can directly inject services that depend on HttpClient in HttpInterceptors

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(private auth: AuthService) 

这篇关于HttpInterceptor-> Service-> HttpClient循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆