Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access Bearer Token on login #9

Open
gyulakiraly opened this issue Nov 9, 2022 · 1 comment
Open

Can't access Bearer Token on login #9

gyulakiraly opened this issue Nov 9, 2022 · 1 comment

Comments

@gyulakiraly
Copy link

Hey there, i found your API, and i would like to use it for a tutorial session. My problem is that, when we make a login call, with axios, we can't access the 'Authorization' header in the response, to save the token in Local Storage. Can you please check your CORS setup so that it allows us to access the header prop?

@richie-chauhan
Copy link

richie-chauhan commented Jun 21, 2023

I added the annotation @CrossOrigin to all 4 rest controllers in src/main/java/com/odazie/todolistapi/webRestControllers/

And then modified src/main/java/com/odazie/todolistapi/security/WebSecurity.java

http.cors().and().csrf().disable().authorizeRequests()
    .antMatchers(HttpMethod.POST, SIGN_UP_URL).permitAll()
    .antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()
    .anyRequest().authenticated()
    .and()
    .addFilter(new JWTAuthenticationFilter(authenticationManager()))
    .addFilter(new JWTAuthorizationFilter(authenticationManager()))
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
    .cors().configurationSource(request -> {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.applyPermitDefaultValues();
        corsConfig.addExposedHeader("Authorization"); // Add the header to be exposed
        return corsConfig;
    });

Not sure that the annotation is required. I did that first, it didn't work by itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants