简体   繁体   中英

Authorization Header in angular 4

I have a server(Spring Boot) that sends me Authorization with a JWT when successful authentication occurs. All of that works fine. I'm able to see that the Authorization header is being returned in the Chromes Network tab. But for some reason When I try to print the headers in angular using the console.log the Authorization header is not showing up. Also the headers.get('Authorization') is not working as well.

login(user) {
this.http
  .post(`${this.url}/login`, user)
  .subscribe((h:Response) => {
    console.log(h.headers.toJSON());  // Doesn't print Authorization header
    console.log(h.headers.get('Authorization')); // prints null
    //this.setToken(h.headers.get('Authorization'));
  });
}

Thanks in advance!

So, after doing a bit more research I ran into this post https://stackoverflow.com/a/43734000/7770219 which answers the questions above. I didn't have my CorsFilters configured properly. Which is why, the frontend(angular 2+) cann't get access to the Authorization token which holds my JWT. Even though you are able to see the token in the postman and Chromes Network tab that doesn't mean that your front end is able to successfully access it. To do that Use the link provided above to give it access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM