Keycloak blocks your single-page app with a CORS error
Access to XMLHttpRequest at 'https://auth.example.com/realms/main/protocol/openid-connect/token' from origin 'https://app.example.com' has been blocked by CORS policyShort answer
Keycloak checks the Origin header of a browser call against the Web origins configured on that client and refuses it with 403 when it does not match. Add the exact origin of your app to the client. If your own API redirects an XHR to the Keycloak login page, the browser reports that as CORS too, so return 401 instead.
Fix it fast
1Find out which cause you have
Call the token or userinfo endpoint with the page's Origin from a terminal. A 403 means Keycloak rejected the origin; a 302 to a login page points at your own API instead.
curl -i https://auth.example.com/realms/main/protocol/openid-connect/userinfo \ -H "Origin: https://app.example.com" -H "Authorization: Bearer invalid"2Match what you see, then jump to its fix
- 1
The browser call to Keycloak fails, and the server answers 403 for a request that carries your originThe app's origin is not listed in the client's Web originsFix - 2
The browser says the request requires preflight and may not follow a cross-origin redirect, and the first step shows a 302Your own API redirects the browser call to the Keycloak login pageFix
- 1
What does the error look like?
Access to XMLHttpRequest at 'https://auth.example.com/realms/main/protocol/openid-connect/token' from origin 'https://app.example.com' has been blocked by CORS policyRequest requires preflight, which is disallowed to follow cross-origin redirects.
Cause 1 of 2
The app's origin is not listed in the client's Web origins
1Check
Keycloak's server code takes the allowed set from the client's configured web origins and compares the incoming Origin header with it; on a mismatch it returns 403. The origin is scheme, host and port only, with no path and no trailing slash.
# the exact value to enter, copied from the failing request's Origin header
echo "https://app.example.com"2Fix
In the admin console open the client used by the app, find the Web origins field in its settings, add the exact origin of the page (one entry per origin, including a dev one such as http://localhost:5173), and save.
Web origins:
https://app.example.com
http://localhost:51733Verify
Reload the app and watch the token request in the network tab; the response now carries Access-Control-Allow-Origin.
curl -s -o /dev/null -D - https://auth.example.com/realms/main/protocol/openid-connect/certs \
-H "Origin: https://app.example.com" | grep -i access-controlConfirmed by keycloak.org
Cause 2 of 2
Your own API redirects the browser call to the Keycloak login page
1Check
A server-side Keycloak middleware may answer an unauthenticated request with a redirect to the login page. MDN says some browsers refuse to follow a cross-origin redirect after a preflighted request, and a request with an Authorization header always triggers a preflight.
curl -i https://api.example.com/orders -H "Origin: https://app.example.com"2Fix
Make the API a bearer-token resource: the app obtains a token with its Keycloak library, sends it in the Authorization header, and the API answers a missing or bad token with 401, never a redirect. Only full page navigations should be sent to the login page.
// Express: reject, do not redirect, for API routes
app.use('/api', (req, res, next) => {
if (!req.headers.authorization) return res.status(401).json({ error: 'unauthenticated' });
next();
});3Verify
The unauthenticated curl now shows 401 and no Location header.
curl -i https://api.example.com/api/orders | head -n 5Confirmed by developer.mozilla.org
How do I stop it coming back?
- Keep one Web origins entry per environment (dev, staging, production) instead of a wildcard.
- Treat your API as a token-protected resource and let only the front end library run login redirects.
- Test the token endpoint with a curl that carries the Origin header after changing a client.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
The browser blocks your API response: no Access-Control-Allow-Origin header
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.
4 causes, each with a fixSecurity
The browser blocks a script because of your Content Security Policy
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
The page's Content-Security-Policy does not allow what it tried to run or load.
5 causes, each with a fixSecurity
jsonwebtoken rejects a token: invalid signature, jwt expired or jwt malformed
JsonWebTokenError: invalid signature
Each message points at a different step.
5 causes, each with a fixSecurity
An HTTPS page is blocked from loading an insecure http:// resource
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but attempted to connect to the insecure endpoint 'http://api.example.com/items'. This request has been blocked.
Your page came over HTTPS but something on it points at an http:// address, and the browser refuses the risky kinds (scripts, styles, frames, fetch calls).
4 causes, each with a fixSecurity
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fix