Angular 6-Auth令牌拦截器未添加标头

kri*_*nya 6 typescript angular

在过去的两天中,我使用Angular 6尝试了许多不同的方法,本文的最新方法是:https : //stackoverflow.com/a/47401544。但是,仍未根据请求设置标头。

import {Inject, Injectable} from '@angular/core';
import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest,
  HttpErrorResponse,
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class AuthTokenInterceptor implements HttpInterceptor {
  constructor() {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).do((event: HttpEvent<any>) => {
      if (localStorage.getItem('id_token') != null) {
        // Clone the request to add the new header.
        const request = req.clone({
          setHeaders: {
            'Content-Type' : 'application/json; charset=utf-8',
            'Accept'       : 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('id_token')}`
          }
        });
        return next.handle(request);
      }
    }, (err: any) => {
      if (err instanceof HttpErrorResponse) {
        if (err.status === 401) {
          console.log('redirect auth interceptor')
          // do a redirect
        }
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我注销request,该request.headers.lazyUpdate数组将更新为3个项目,但看不到Authorization在拦截的请求标题。

request.headers.lazyUpdate

{name: "Content-Type", value: "application/json; charset=utf-8", op: "s"}
{name: "Accept", value: "application/json", op: "s"}
{name: "Authorization", value: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ2Mzh9.tLTmPK46NhXSuqoCfZKgZcrQWzlNqLMI71-G0iy3bi8", op: "s"}
Run Code Online (Sandbox Code Playgroud)

request.headers.headers是空的-这可能是问题吗?)

app.module.ts:

providers: [
    {provide: HTTP_INTERCEPTORS, useClass: AuthTokenInterceptor, multi: true},
  ],
Run Code Online (Sandbox Code Playgroud)

导致我认为这是拦截器的问题是,如果我手动将标头添加到请求中,则不会得到a 401并且请求返回正确的数据和a 200

return this.http.get(environment.API_URL + 'list/supervise/' + encodeURIComponent(id),
      {headers: new HttpHeaders().set('Authorization', `Bearer ${localStorage.getItem('id_token')}`)}).pipe(
        map((res: any) => res.data)
    );
Run Code Online (Sandbox Code Playgroud)

我有什么可以忽略的吗?谢谢。

编辑:

正如我在下面的评论中提到的,我回来了next.handle两次。这是我最终使用的解决方案:

import {Injectable} from '@angular/core';
import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class AuthTokenInterceptor implements HttpInterceptor {
  constructor() {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token = localStorage.getItem('id_token');

    req = req.clone({
      setHeaders: {
        'Authorization': `Bearer ${token}`
      },
    });

    return next.handle(req);
  }
}
Run Code Online (Sandbox Code Playgroud)

lup*_*upa 3

你可以尝试一个更简单的版本。( just like your reference link does)

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const jwt = localStorage.getItem('id_token');
    if (!!jwt) {
     req = req.clone({
       setHeaders: {
         Authorization: `Bearer ${jwt}`
       }
     });
   }
   return next.handle(req);
 }
Run Code Online (Sandbox Code Playgroud)

您不必处理此处,error因为此处(在您的上下文中)
的重点是克隆(这意味着每当我们收到请求时,我们都会克隆它,然后做任何我们想做的事情并将其发送出去)。 我们可以添加更多的标头 和更多的数据,它将被发送出去,然后最终从 Api 返回 ,并将句柄问题留给调用的人(例如:,...)。intercepter


servicehttpRequestthen, catch, pipe

再次声明,您的应用程序中的 to api的app.module.ts意思将被拦截,如果我想处理带有错误消息的特定请求怎么办?如果您执行一些复杂的逻辑,它可能会影响所有请求。 关于上面的代码,我还没有尝试过,但我认为当您像这样嵌套时可能会发生错误,您应该在其上设置断点并尝试调试发生的情况。allrequestNothing here