【发布时间】:2019-12-12 23:19:18
【问题描述】:
我不知道为什么我无法从 Postman 中看到的标题 AUTHORIZATION 中获取值(从服务器返回)。
http://img110.xooimage.com/files/1/6/9/postman-567005e.png
我尝试了很多东西,但我不知道为什么我仍然得到一个空值。
http://img110.xooimage.com/files/b/c/f/debug-5670075.png
这是我的代码:
身份验证服务.ts
login(u: User): Observable<HttpResponse<Response>> {
if ((u.username && u.password)) {
this.user.setUsername(u.username);
this.user.setPassword(u.password);
// @ts-ignore
const request = this.http.post<Response>(this.authUrl, this.user, {observe: 'response'} )
.pipe(
tap((data: any) => {
// @ts-ignore
this.log(`Succès ${data.status} authentification succès`, 'success');
this.navigateToCollection();
console.log('data');
console.log(data);
console.log('data.headers.get(Authorization)');
console.log(data.headers.get('Authorization'));
return localStorage.setItem(TOKEN_KEY, data.headers.get('Authorization'));
}),
catchError(this.handleError<any>('login',
console.log('je passe ici3')))
);
request.subscribe(value => {
// console.log(value.headers.get('Authorization'));
console.log(localStorage.getItem(TOKEN_KEY));
});
this.presentLoading(request);
console.log('je passe ici4');
return request;
} else {
this.log('Renseignez les champs', 'error');
alert('Erreur de login ou de mot de passe');
}
}
我的令牌是通过我的服务身份验证(spring boot)发送的,如果您需要更多信息,请咨询我。
我想知道如何获取这个标头值。
感谢您的宝贵时间。
【问题讨论】:
-
您无法从数据中获取授权标头。
-
data和headers在两个不同的对象中可用。 -
"响应中的授权标头不是标准的,因此服务器必须明确通知公开它。为此,服务器必须添加以下标头:Access-Control-Expose-Headers:授权"来源:stackoverflow.com/questions/44032464/…
-
或者您可能必须在服务器端执行此操作:
response.addHeader("access-control-expose-headers", "Authorization"); -
您是否向其他域发出请求?如果是,那么它是一个 CORS 请求,您必须在响应中添加上述标头。
标签: javascript spring-boot angular7 bearer-token