简体   繁体   English

如何在角度4中添加自定义拦截器

[英]How to add custom interceptor in angular 4

I have written a custom interceptor on angular 4. The issue I am having is checking URL with dynamic path variable in the URL. 我在angular 4上编写了一个自定义拦截器。我遇到的问题是使用URL中的动态路径变量检查URL。 The backend application has Oauth2 and it's deployed in a separate server(Authorization and Resource). 后端应用程序具有Oauth2,并且已部署在单独的服务器(授权和资源)中。 The interceptor class of the angular is below 角的拦截器类别如下

export class TokenInterceptor implements HttpInterceptor {
    private whiteList = [
        'http://localhost:8081/Test1app/oauth-server/oauth/token',
        'http://localhost:8080/ResourceApp/oauth-resource/ibex/api/signup',
        'http://localhost:8080/ResourceApp/oauth-resource/ibex/api/registeration/confirm/**',
        'http://localhost:8080/ResourceApp/oauth-resource/ibex/api/user/profile/upload',
        'http://localhost:8080/ResourceApp/oauth-resource/ibex/api/profile/edit/password/**'
    ]
    constructor(public auth : AuthenticationService) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        console.log(request.url);
        // If request is not in whitlist add header
        if (!this.whiteList.includes(request.url)) {
            console.log("I was not here");
            console.log(request.url);
            request = request.clone({
                setHeaders: {
                    Authorization: `Bearer ${this.auth.getToken()}`
                }
            });
        }

        return next.handle(request);
    }

}

the issue that I am facing now is whenever I try to access http://localhost:8080/ResourceApp/oauth-resource/ibex/api/profile/edit/password/12345-2333-4444 it's going under the if condition (request is in the whitelist) 我现在面临的问题是,每当我尝试访问http:// localhost:8080 / ResourceApp / oauth-resource / ibex / api / profile / edit / password / 12345-2333-4444时,它都处于if条件(请求在白名单中)

Try this: 尝试这个:

export class TokenInterceptor implements HttpInterceptor {
  private patterns = [
    new RegExp('http://localhost:8081/Test1app/oauth-server/oauth/token'),
    new RegExp('http://localhost:8080/ResourceApp/oauth-resource/ibex/api/signup'),
    new RegExp('http://localhost:8080/ResourceApp/oauth-resource/ibex/api/registeration/confirm/.*'),
    new RegExp('http://localhost:8080/ResourceApp/oauth-resource/ibex/api/user/profile/upload'),
    new RegExp('http://localhost:8080/ResourceApp/oauth-resource/ibex/api/profile/edit/password/.*')
  ];

  constructor(public auth : AuthenticationService) {}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (!this.match(request.url)) {
      request = request.clone({ setHeaders: { Authorization: `Bearer ${this.auth.getToken()}` } });
    }

    return next.handle(request);
  }

  match(url: string): boolean {
    for (const pattern of this.patterns) {
      if (Array.isArray(url.match(pattern))) { return true; }
    }

    return false;
  }

}

References: 参考文献:

String.prototype.match() String.prototype.match()

Regular Expressions 常用表达

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM