Ril*_*n42 6 http-headers angular-http-interceptors angular
我已经通过 Angular 中的 HttpInterceptor 添加了授权标头,但输出似乎不正确。根据添加标头后的 JSON 输出,这感觉太笨拙而无法正确。我是否错误地添加了标头,或者我访问它们是否错误?:
headers["lazyUpdate"].forEach(x=>{x.name=='Authorization'}).value
Run Code Online (Sandbox Code Playgroud)
目标是回归Bearer mystring
拦截器调用:
{"url":" https://myurl ","body:{}, "reportProgress":false, "withCredentials":false, "responseType":"json", "method":"POST", "headers" :{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map" :null},"urlWithParams":"myurl"}
token.interceptor.ts:13拦截器添加token:
{"url":"myurl", "body":{}, "reportProgress":false, "withCredentials":false, "responseType":"json", "method":"POST", "headers":{" NormalizedNames":{},"lazyUpdate":[ {"name":"Authorization","value":"Bearer mystring","op":"s"} ], "headers":{},"lazyInit": {"normalizedNames":{},"lazyUpdate":null,"headers":{}}},"params":{"updates":null,"cloneFrom":null,"encoder":{},"map" :{}},"urlWithParams":"myurl"}
我的 TokenInterceptor 类:
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('interceptor called: '+JSON.stringify(request))
let r = request.clone({
headers: request.headers.set('Authorization', `Bearer ${localStorage.getItem("id_token")}`)
});
console.log('interceptor added token: '+JSON.stringify(r))
return next.handle(r);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1025 次 |
| 最近记录: |