如上所述。 以下代码不起作用:
public login() {
const username = "kamczi";
const password = "password";
return this.http.post<any>(environment.api_endpoint+'/login', { username, password} , {observe: 'response'}).subscribe(resp=>{
this.handleAuthentication(resp.headers.get('Authorization'))
})
}
public handleAuthentication(token: string): void {
localStorage.setItem('access_token', token);
console.log(token);
}
Console.log
显示空
我需要获取位于响应标头中的授权标头。
解决
我需要将以下代码添加到我的 Spring Boot 安全配置中。
CorsConfiguration configuration = new CorsConfiguration();
configuration.applyPermitDefaultValues();
configuration.addExposedHeader("Authorization"); // this line helped
您的 Angular 代码运行良好。有些标头只能被浏览器拦截,用户无法在代码内部访问它们。为此,您必须在回复中明确暴露它们。
因此,当您从 API 发送响应时,您需要将以下标头添加到响应中,并且
Authorization
标头将可用。
'access-control-expose-headers' : 'Authorization'