我有一个 HttpInterceptor 可以将本地存储中的 JWT 令牌添加到我的 http 请求的标头中。我相信我所有的 http 请求都是在没有任何标头的情况下发送的,但不明白为什么它不起作用。
我缺少什么让拦截器将标头添加到我的请求中?
我的拦截器:
import { Injectable, Injector } from '@angular/core';
import {HttpInterceptor} from "@angular/common/http";
@Injectable({
providedIn: 'root'
})
export class TokenInterceptorService implements HttpInterceptor {
constructor(private injector: Injector) { }
intercept(req, next) {
let tokenizedReq = req.clone({
setHeaders: {
Authorization: `Bearer ${localStorage.getItem('token')}`
}
})
return next.handle(tokenizedReq)
}
}
我的服务:
import { baseUrl } from "../environments/environment";
import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class AccountService {
constructor(private http:HttpClient) { }
getUser(data):Observable<any>{
return this.http.post(`${baseUrl}/useraccount/user`, data);
}
}