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

Security - Implementation #22

Closed
vivdso opened this issue Feb 21, 2017 · 6 comments
Closed

Security - Implementation #22

vivdso opened this issue Feb 21, 2017 · 6 comments
Labels

Comments

@vivdso
Copy link

vivdso commented Feb 21, 2017

Thanks for putting this online, I did try to implement this in my project, Sending JWT tokens as my Header.
for some reason, I am getting 403 forbidden error. I am not sure, what I am doing wrong or if I am missing any configuration. I have put the project on git hub, If someone can point me what the issue is.
https://github.com/vivdso/SpringAuthentication

Please assist.

@szerhusenBC
Copy link
Owner

Please give some more information. Which request returns the 403?

Take a look at your WebSecurityConfig. There are some things missing that I have in my WebSecurityConfigurerAdapter. Perhaps you get a better understanding of how this demo works if you take a look at the video I linked in the Readme.

@vivdso
Copy link
Author

vivdso commented Feb 21, 2017

Sure, I will look at it.
Any API call fails, I have two of them below is the API calls

http://localhost:8080/auth method post Content-Type appliction/json body:{"username":"user","password":"sample"} Response should be a jwt token

Try the autheticated url: http://localhost:8080/order
Header "Authorization":"{$jwtToken from previous step}"
Actual Result: :( Error : 403 forbidden, this should be fully authenticated and should let the user access this api.
Expected Result: "Hello here is my order"

@jsbUSMC
Copy link

jsbUSMC commented Feb 24, 2017

Shouldn't the header be set to:

Authorization: Bearer {$jwtToken}

I'm not sure if this detail is important or not, but from my understanding of the specification, JWTs should be declared as Bearer tokens in the Authorization header.

@szerhusenBC
Copy link
Owner

I didn't find the need of the bearer scheme in any specification but it seems to me that it is the typical way to mark a bearer token. I will adapt it to the project with another ticket.

@tandrew
Copy link

tandrew commented Sep 6, 2018

The current version did not work for me when trying to access it from another domain. It produced the following error:

image

Failed to load http://localhost:8080/auth: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.

I spent quite some time figuring out what to do here. I found a solution and have added this to WebSecurityConfig.java:

This then fixed it for me:

@Bean
   public CorsFilter corsFilter() {

       UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
       CorsConfiguration config = new CorsConfiguration();
       config.setAllowCredentials(true); // you USUALLY want this
       config.addAllowedOrigin("*");
       config.addAllowedHeader("*");
       config.addAllowedMethod("OPTIONS");
       config.addAllowedMethod("HEAD");
       config.addAllowedMethod("GET");
       config.addAllowedMethod("PUT");
       config.addAllowedMethod("POST");
       config.addAllowedMethod("DELETE");
       config.addAllowedMethod("PATCH");
       source.registerCorsConfiguration("/**", config);
       return new CorsFilter(source);
   }

Credit to: https://stackoverflow.com/questions/36809528/spring-boot-cors-filter-cors-preflight-channel-did-not-succeed

Thought I'd share as CORS issues seem to be pretty common all around.

@szerhusenBC
Copy link
Owner

@tandrew Thanks for sharing your solution!

Here's another possibility from a Spring guide: https://spring.io/guides/gs/rest-service-cors/

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

No branches or pull requests

4 participants