从Http Interceptor获取授权头

时间:2017-11-05 09:39:09

标签: angular header authorization interceptor angular-http-interceptors

我已授权将令牌直接放入Http Interceptor。我现在如何从Angular 4 console logs获得此令牌?

enter image description here

不幸的是, 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 } }); } 以下的所有内容都不包含此标题:

query = session.query(User)
def query_to_dict(query):
        def _create_dict(r):
            return {c.get('name'): getattr(r, c.get('name')) for c in query.column_descriptions}

    return [_create_dict(r) for r in query]

1 个答案:

答案 0 :(得分:2)

HttpHeadersMap instance的包装器,因此现有标头不会显示在console.log输出中,因为Map值未公开。

可以从HttpHeaders实例中检索标头,因为其API建议:

req.headers.get('authorization');