-
Notifications
You must be signed in to change notification settings - Fork 6k
Single page application (SPA) not redirected to OAuth2 provider via Spring Gateway #10843
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
Comments
Hi @straurob. Thanks for your interest in the project! I've added a comment for possible resources to check out to work through the CORS issue. For reference, it's not required to cross-post from Stack Overflow as the team monitors the |
@razvanmazilu99 I managed to find a solution. It is not related to Spring Security, but I will post the approch here anyway for documentation purposes. The problem, as far as I can tell, was that the frontend application issued requests using the axios library. When such a request hits a secured resource method, Spring Security will answer with a redirect to Keycloak. Redirects won't work in this case as it's the browser itself which needs to actually call I solved this by catching any 401 response in my frontend application and then redirecting the user agent. Something like this: if (error.response.status === 401) {
redirect('http://localhost:8500/oauth2/authorization/keycloak');
} |
@straurob thank you very much!!! |
@straurob Thank you for your fine compiled question. The way you structuring spring security apps and the way you debug things is quite inspiring to me. I'm researching how to implement the same architecture with new Spring Authorization Server, Spring Cloud Gateway and Vue3. 😃 |
Preface
I'm trying to setup a proof-of-concept like in the following diagram:
Observations
Scenario 1 (browser without SPA)
When I use the browser without the SPA, then the following workflow works as intended:
http://localhost:8555/api/messages
.http://localhost:8555/api/messages
and aSESSION
cookie is created./api/messages
is shown in the browser.Scenario 2 (browser with SPA)
This is the scenario I'd like to actually realize. The observed workflow here is:
http://localhost:8093
.http://localhost:8555/api/messages
.Instead, there a several messages in the browser console indicating CORS issues.
Console
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/auth/realms/demo/protocol/openid-connect/auth?response_type=code&client_id=my-client&state=vpgnHaxh-5nb_LxksYL1a-PRU0tzuHR5itVvw1ovD-E%3D&redirect_uri=http://localhost:8555/login/oauth2/code/keycloak. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
Network Traffic
As far as I can see, the following is happening:
http://localhost:8555/api/messages
is answered with a 302 and location/oauth2/authorization/keycloak
.http://localhost:8555/oauth2/authorization/keycloak
is answered with a 302 andLocation: http://localhost:8080/auth/realms/demo/protocol/openid-connect/auth?response_type=code&client_id=my-client&state=vpgnHaxh-5nb_LxksYL1a-PRU0tzuHR5itVvw1ovD-E%3D&redirect_uri=http://localhost:8555/login/oauth2/code/keycloak
http://localhost:8080/auth/realms/demo/protocol/openid-connect/auth?response_type=code&client_id=my-client&state=vpgnHaxh-5nb_LxksYL1a-PRU0tzuHR5itVvw1ovD-E%3D&redirect_uri=http%3A%2F%2Flocalhost%3A8555%2Flogin%2Foauth2%2Fcode%2Fkeycloak
is answered with 200.But all of them have CORS issues.
Spring Boot Implementation
Gateway
application.yml
SecurityConfiguration
Keycloak Client
The text was updated successfully, but these errors were encountered: