Angular应用从另一个应用重定向。所以我们为angular应用添加了头文件来实现单点登录选项。但是无法在angular应用的拦截器类中读取传入的请求头。
注意:但是如果我们使用MVC应用程序,可以访问相同的头值。
@Injectable()
export class HeaderInterceptor implements HttpInterceptor {
uid: any = "";
err!: Error;
constructor(private errorDialogService: ErrorDialogService,
private router: Router,
private route: ActivatedRoute,
private log: LoggingService,
private paydetservice: PayDetailsService) { }
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.uid = req.headers.get('userid');
}
}
所以,你在这里做的是读取请求头:你的angular应用程序发送给服务器的头。你要做的是读取响应头:从服务器到angular应用。
你可以这样做:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
tap((httpEvent: HttpEvent<any>) => {
if (httpEvent instanceof HttpResponse) {
console.log(httpEvent.headers);
}
})
);
}
但是我不确定你是否真的想在拦截器中这样做。通常在使用HttpClient
执行请求时更简单