gpt4 book ai didi

javascript - Angular jwt 无效 token

转载 作者:行者123 更新时间:2023-11-30 08:25:07 25 4
gpt4 key购买 nike

我有 Angular JWT token 的问题,它适用于我来自 Postman 的 api,但不适用于 Angular 调用。索取详情如下:screen shot

我复制了响应中显示的 token 并通过 Postman 生成了一个有效的请求。

知道为什么这个 token 在从 Angular 服务发送时可能无效吗?

Postman 结果如下:

enter image description here

这里是生成 token Angular 边的

    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');
}

最佳答案

授权 header 通常采用这种格式:

Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

看起来您正在尝试发送整个对象。仅发送 token 字段。

关于javascript - Angular jwt 无效 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47018100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com