SPA + Quarkus + Keycloak #38456
-
Hi, Now, there is the issue that my SPA also uses the fetch API to send Restful request to the Quarkus backend and it can happen that the OpenID session is expired and in that cases, the fetch will return 302 with the redirect URL to Keycloak. But neither can i access the redirect location and handle it in Javascript explicitly (which seems to be a known limitation by the browser; the browser will follow the redirect himself. No way to get the Location header in the 302 response via Javascript) nor is the fetch internally able to follow the redirect due to CORS. This is the CORS error i see in the browser (Chrome):
I was thinking that in keycloak i maybe have to set the Weborigins so it returns appropriate CORS headers so the fetch internally will follow the redirect and then i can get the redirected URL and do a top-level reload of the browser to the keycloak.
No matter what i configure here for the web origins, keycloak never returns CORS headers in the response. Anyone can suggest to me if I am on the right track or not and what might be my issue? Maybe i am doing something fundamentally wrong but it seems like this is the suggested approach to handle this. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
/cc @pedroigor (keycloak), @sberyozkin (keycloak) |
Beta Was this translation helpful? Give feedback.
-
@38leinaD If you have SPA which delegates to Quarkus to do the authorization code flow then the only way to manage CORS at the Fetch level is to follow the workaround described at: https://quarkus.io/guides/security-oidc-code-flow-authentication#single-page-applications. Keycloak Authorization endpoint does not support CORS so the workaround is to let Quarkus return an error instead of the redirect so that the error catch block could retry the request with the browser API, thus bypassing the CORS restriction. Give it a try please |
Beta Was this translation helpful? Give feedback.
-
How did I miss that part of the documentation? Thanks @sberyozkin! |
Beta Was this translation helpful? Give feedback.
@38leinaD If you have SPA which delegates to Quarkus to do the authorization code flow then the only way to manage CORS at the Fetch level is to follow the workaround described at: https://quarkus.io/guides/security-oidc-code-flow-authentication#single-page-applications.
Keycloak Authorization endpoint does not support CORS so the workaround is to let Quarkus return an error instead of the redirect so that the error catch block could retry the request with the browser API, thus bypassing the CORS restriction. Give it a try please