简体   繁体   English

角度jwt无效令牌

[英]angular jwt invalid token

I have this issue with angular JWT token, which works on my api from Postman but not from the angular call. 我遇到了与角度JWT令牌有关的问题,该问题在Postman的我的api上有效,但在角度调用中不起作用。 Request details as follow: 请求详细信息如下: 屏幕截图

I copied the token showed in the response and generate a request through Postman which works. 我复制了响应中显示的令牌,并通过邮递员生成了一个有效的请求。

Any idea why this token could be invalid when sent from angular service? 知道为什么从角度服务发送时该令牌可能无效吗?

Postman result below: 邮递员结果如下:

在此处输入图片说明

here is the code that produces the token angular side> 这是产生令牌角边的代码>

    import { Injectable } from '@angular/core';

    import {
      HttpRequest,
      HttpHandler,
      HttpEvent,
      HttpInterceptor} from '@angular/common/http';
    //import { AuthService } from './auth/auth.service';
    import { Observable } from 'rxjs/Observable';
    import { Http } from "@angular/http/http";
    import { AuthService } from "./auth.service";
    import { HttpClient } from "@angular/common/http";
    @Injectable()
    export class TokenInterceptor implements HttpInterceptor {
      constructor(public auth: AuthService) { }
      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        request = request.clone({
          setHeaders: {
            Authorization: `Bearer ${this.auth.getToken()}`
          }
        });
        return next.handle(request);
      }
    }

  public getToken(): string {
    return localStorage.getItem('token');
  }

The authorization header is usually on this format: 授权标头通常采用以下格式:

Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

It looks like you're trying to send an entire object. 看来您正在尝试发送整个对象。 Send only the token field instead. 仅发送令牌字段。

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

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