首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Angular中基于请求Uri的自定义拦截器和Msal拦截器之间使用条件拦截器

如何在Angular中基于请求Uri的自定义拦截器和Msal拦截器之间使用条件拦截器
EN

Stack Overflow用户
提问于 2021-04-21 15:00:05
回答 1查看 429关注 0票数 1

我已经在我的angular 11项目中实现了@azure/msal-angular 2.0。我已经在这个项目中使用了2个Apis。很少有APis需要自定义拦截器(AuthInterceptor),也很少需要MsalInterceptor by msal.js。两个API一次只能使用一个Interceptor,否则请求将失败。我想知道如何在一个拦截器中定制MsalInterceptor和CustomInterceptor。

这是我的app.module.ts文件(在这里,我想删除msalinterceptor并在AuthInterceptor中定制它,或者我想把两个拦截器都保留在这里,并根据请求URI有条件地使用它)

代码语言:javascript
运行
复制
  providers: [
{
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true
},
{
  provide: HTTP_INTERCEPTORS,
  useClass: MsalInterceptor,
  multi: true
},  
{
  provide: MSAL_INSTANCE,
  useFactory: MSALInstanceFactory
},
{
  provide: MSAL_GUARD_CONFIG,
  useFactory: MSALGuardConfigFactory
},
{
  provide: MSAL_INTERCEPTOR_CONFIG,
  useFactory: MSALInterceptorConfigFactory
},
MsalService,
MsalGuard,
MsalBroadcastService

],

这就是我在这个文件中使用MsalInterceptor定制AuthInterceptor的方法。

代码语言:javascript
运行
复制
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {      
    if (req.url.indexOf(this.APIOne) !== -1) {

      // I want to use USE MSALInterceptor Here. but it is not working.

              const scopesInformation = {
                        scopes: [
                             'mail.send'
                        ],
                     };
                       
                    this.auth.instance.acquireTokenSilent(scopesInformation).then(tokenResponse => {
                             debugger;
                             req = req.clone({
                                 setHeaders: { Authorization: 'Bearer ' + tokenResponse.accessToken }
                            });
                         });
                     
    } else
        if (req.url.indexOf(this.APITwo) !== -1) {
          // This is my custom interceptor which does not require MsalInterceptor Token.
            this.token = localStorage.getItem('APITwoToken');
            const authReq = req.clone({
                headers: new HttpHeaders({ 
                   'Cache-Control': 'no-cache, no-store, must-revalidate, post-check=0, pre-check=0',
                    'Pragma': 'no-cache',
                    'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
                }), setHeaders: { Authorization: 'Bearer ' + this.token }
            })
            req = authReq;
        }

    return next.handle(req).pipe(
        tap(
            event => this.handleResponse(req, event),
            error => this.handleError(req, error)
        )
    );
}

任何帮助都是非常感谢的。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-04-21 19:23:50

想出了一个临时的解决方案。刚把MSALInterceptor移到了AuthInterceptor上面。不知道这是不是一个好的解决方案,但不知何故,这是有效的。

代码语言:javascript
运行
复制
{
  provide: HTTP_INTERCEPTORS,
  useClass: MsalInterceptor,
  multi: true
},
{
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67190747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档