如果有 401 响应,角度拦截器会注销应用程序吗?

Posted

技术标签:

【中文标题】如果有 401 响应,角度拦截器会注销应用程序吗?【英文标题】:angular interceptor logout the app if there is an 401 response? 【发布时间】:2019-07-22 08:40:20 【问题描述】:

在我的拦截器中,如果响应 statusCode 为 401,我想在响应上放置一个管道并注销,但问题是当我放置一个 catchError 时,observable 不是 HttpEvent 类型并且拦截函数期望返回该类型?

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> 
    const authToken = `Agency $this.auth.token`;
    const requestWithHeaders = req.clone( setHeaders: 'Content-Type': 'application/json');
    if (!!this.auth.token) 
        const authReq = requestWithHeaders.clone( setHeaders:  Authorization: authToken );
        return next.handle(authReq).pipe(
            catchError(er => 
                if (er.status === 401) 
                    this.auth.logout();
                
                return er;
            )
        );
     else 
        return next.handle(requestWithHeaders);
    

【问题讨论】:

创建注销路径并导航。 【参考方案1】:

抛出一个新的错误,它将被转发而不是仅仅返回错误:

import  Observable, throwError  from 'rxjs';
import  catchError  from 'rxjs/operators'

...

catchError(er => 
    if (er.status === 401) 
        this.auth.logout();
    
    return throwError(er);
)

【讨论】:

【参考方案2】:

您可以使用以下方法处理您的Error 回复,试试这个

   return next.handle(clonedRequest).do((event: HttpEvent<any>) => , (error: any) => 
      if (error instanceof HttpErrorResponse) 
           if (error.status == 401 || error.status == 403) 
             this.auth.logout();
         
      
   );

我希望这能解决你的问题:)

【讨论】:

此代码适用于旧版本的 rxjs 新版本是 tap 并且应该在管道内。 tx 为您提供帮助 @Az264 我建议在pipe() 中使用catchError(),在这种情况下比tap(()=&gt;, (error:any)=&gt;) 更优雅。【参考方案3】:

我就是这样做的。

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> 
    //   const  newReq= req.clone(headers: req.headers.set('source', 'WEB'));
  const  newReq = req.clone(
        headers: new HttpHeaders(
            'source': 'WEB',
            'Authorization': localStorage.getItem(TOKEN) || ''
        )
    );
    return next.handle(newReq).pipe(map((e: HttpEvent<any>) => 
        if (e instanceof HttpResponse) 
    /*        console.error(e);
            console.log(e.status);*/
            if (e.status === 401) 
                throw new Error('invalid token');
            
        
        return e;
    ));

if (e instanceof HttpResponse) this me that if its response>> 如果它的响应那么做你喜欢的验证。这里我只是抛出错误但我们可以清除保存的令牌并重定向到登录页面

【讨论】:

以上是关于如果有 401 响应,角度拦截器会注销应用程序吗?的主要内容,如果未能解决你的问题,请参考以下文章

AngularJS成功401拦截还是抛出401

axios 拦截器响应未定义

Angular 4 和 OAuth - 拦截 401 响应,刷新访问令牌并重试请求

使用 axios 拦截器时,API 调用循环出现 401 错误

尝试在http拦截器中捕获401,刷新我的会话并重试原始请求

Angular 7 - 如何在某些响应状态代码上重试 http 请求?