【发布时间】:2018-06-22 06:05:16
【问题描述】:
我有启用 Spring-Boot JWT 的 REST API。身份验证 API /login 确实按预期提供了所有 CORS 和 AUTH 标头。通过 Curl 和 Postman 测试。 我的客户端代码在 Angular 5 中。成功通过身份验证后,我可以看到 -
- 所有必需的标头,特别是网络流量中 Chrome 开发者工具中的“授权”标头。
- 但是,当使用 Angular 5 HttpClient 访问时,组件/服务中无法访问 Header。
注意:后端、CORS 和允许标头已启用,因此 Chrome 网络流量正常。
在 POST 调用期间在 HttpClient 中添加了所需的选项。
{
responseType: 'text',
observe: 'response'
}
在那里尝试了所有可能的 responseType。 默认情况下,JSON 会引发解析异常。 这意味着默认的 Spring-Boot JWTS /login API 不返回 JSON 输出,而只返回 HttpResponse。
请帮忙...
Angular 代码 -
login(payload: any): Observable < any > {
return this._http.post(this.loginEndPoint, payload, {
responseType: 'text',
observe: 'response'
})
/* .map((response: HttpResponse<string>) => {
console.log(response.headers.get('Authorization'));
return response;
}); */
.pipe(
tap(response => {
console.log(response.headers.get('Authorization'));
return response;
}),
catchError(this.handleError('getHeroes', []))
);
}
【问题讨论】:
-
你好,我来晚了。更令人困惑的是,我能够在 Postman 和 Chrome 开发者控制台中获得标题,但在 Angular 中却没有。最初我认为这是因为 Angular 内部使用了 Angular OR Observable OR XMLHttpRequest。事情很简单,只需添加 res.setHeader("Access-Control-Expose-Headers", "Authorization");我也在尝试以下不起作用 res.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept, x-requested-with, Cache-Control");谢谢,我希望它会帮助别人。
标签: angular spring-boot jwt