Issue
As above. Following code doesn't work:
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
displays null
I need to get my authorization header which is located in my response header.
SOLVE
I needed to add following code to my spring boot security conf.
CorsConfiguration configuration = new CorsConfiguration();
configuration.applyPermitDefaultValues();
configuration.addExposedHeader("Authorization"); // this line helped
Solution
Your Angular code is working fine. Some headers are meant to be intercepted by browsers only and user can't access them inside code. To do so you have to explicitly expose them in your response.
So you need to add the following header to the response when you send it from your API and the Authorization
header will be available.
'access-control-expose-headers' : 'Authorization'
Answered By - asimhashmi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.