augenss
augenss

Reputation: 71

Spring Security JWT - Postman authentication through Authorization header works but from my Angular front end doesn't work

I have a web application with Java, Spring Boot and Spring Security on the backend, and Angular on the front end.

I have implemented login with Spring Security and the authentication seems to work - in Postman as well as in my application's front end I receive a jwt token. Both tokens seem to be correct, because no matter if I use the token acquired in Postman or in front end, when I send any of these tokens in Authorization header when making other requests to the server, the token is accepted by my application's back end and I get 200 OK response from the server.

But here's where things go separate ways: my request is only authorized when I send it from Postman. When I send (I think)the same request from my app's front end, back end throws 403 forbidden. Naturally I assumed that I must send the request differently from my front end than I send from Postman, but I just can't find any difference. From what I see I send the same method, same body, same headers (with the authorization token)

Do you have any idea what might be going on there?

Upvotes: 0

Views: 918

Answers (1)

augenss
augenss

Reputation: 71

The reason for that was that I needed to enable cors in Spring Security in the class extending the WebSecurityConfigurerAdapter :

protected void configure(HttpSecurity http) throws Exception {
    http
            .cors().and()

Upvotes: 1

Related Questions