Angular 从Http侦听器获取授权标头
我已进行授权,该授权将令牌直接放入Angular 从Http侦听器获取授权标头,angular,header,authorization,interceptor,angular-http-interceptors,Angular,Header,Authorization,Interceptor,Angular Http Interceptors,我已进行授权,该授权将令牌直接放入标题。我现在如何从Angular 4Http拦截器获取此令牌 不幸的是,以下控制台日志中没有一个包含此标题: intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { //const changedReq = req.clone({headers: req.headers.set('Authorization
标题
。我现在如何从Angular 4Http拦截器获取此令牌
不幸的是,以下控制台日志中没有一个包含此标题:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
//const changedReq = req.clone({headers: req.headers.set('Authorization', 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbkB3cC5wbCIsImV4cCI6MTUxMDY2NDM0M30.0iBktdr4-1EzTi1iaQOOfguK7HGVJF7JYkB-AF3uZgJrmKnVESAyKkHoNRzum1Pq5xZ6GJaZC9cbZQ2umMSfLA')});
console.log('req', req);
return next.handle(req).do((event: HttpEvent<any>) => {
console.log('event', event);
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
this.ehs.setService(err.status, err.error);
// redirect to login
}
});
}
intercept(req:HttpRequest,next:HttpHandler):可观察{
//const changedReq=req.clone({headers:req.headers.set('Authorization'、'bearrier eyJhbGciOiJIUzUxMiJ9.eyjzwioijhzg1pbkb3cc5wbcisimv4cci6mtuxmdy2ndm0m30.0iBktdr4-1ezti1aqoofguk7hgvjf7jykb-af3uzjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj;
控制台日志('req',req);
返回next.handle(req).do((事件:HttpEvent)=>{
console.log('event',event);
if(HttpResponse的事件实例){
//如果你想的话,做一些有回应的事情
}
},(错误:任何)=>{
if(HttpErrorResponse的错误实例){
此.ehs.setService(err.status,err.error);
//重定向到登录
}
});
}
是的包装器,因此现有标题不会显示在控制台中。日志输出,因为Map
值不会公开
可以从HttpHeaders
实例检索头,正如其API所示:
req.headers.get('authorization');
尝试在HttpResponse分支中记录event.headers