[英]How to get response header from POST request repsonse
如上。 以下代碼不起作用。
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 Security conf中添加以下代碼。
CorsConfiguration configuration = new CorsConfiguration();
configuration.applyPermitDefaultValues();
configuration.addExposedHeader("Authorization"); // this line helped
您的Angular代碼工作正常。 某些標頭僅應被瀏覽器截獲,並且用戶無法在代碼內訪問它們。 為此,您必須在響應中顯式公開它們。
因此,當您從API發送響應時,您需要在響應中添加以下標頭,然后才能使用Authorization
標頭。
'access-control-expose-headers' : 'Authorization'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.