<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Garth</title>
    <description>The latest articles on DEV Community by Garth (@xgp).</description>
    <link>https://dev.to/xgp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1131095%2Ffe7021e6-20cc-4a47-a05a-58d4b25df501.png</url>
      <title>DEV Community: Garth</title>
      <link>https://dev.to/xgp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xgp"/>
    <language>en</language>
    <item>
      <title>Keycloak infinite redirect loop at login</title>
      <dc:creator>Garth</dc:creator>
      <pubDate>Thu, 03 Dec 2026 09:30:41 +0000</pubDate>
      <link>https://dev.to/xgp/keycloak-infinite-redirect-loop-at-login-4cbg</link>
      <guid>https://dev.to/xgp/keycloak-infinite-redirect-loop-at-login-4cbg</guid>
      <description>&lt;p&gt;A Keycloak infinite redirect loop at login almost always means one thing: a cookie Keycloak set on the way to the login page did not come back on the way out of it. Keycloak has no session to resume, so it starts the flow again, and the browser bounces between your application and &lt;code&gt;/protocol/openid-connect/auth&lt;/code&gt; until it gives up.&lt;/p&gt;

&lt;p&gt;The reason this is so common is in the &lt;code&gt;Set-Cookie&lt;/code&gt; headers. Keycloak 26.8.0 marks &lt;strong&gt;every&lt;/strong&gt; cookie in the login flow &lt;code&gt;Secure; SameSite=None&lt;/code&gt;, unconditionally — there is no setting that relaxes it. A browser will not store a &lt;code&gt;Secure&lt;/code&gt; cookie that arrives over plain &lt;code&gt;http://&lt;/code&gt;, and it will not store &lt;code&gt;SameSite=None&lt;/code&gt; without &lt;code&gt;Secure&lt;/code&gt;. So the loop is usually not an OIDC problem at all. It is a transport problem that the OIDC layer reports as a lost session.&lt;/p&gt;

&lt;p&gt;Everything below was reproduced against Keycloak 26.8.0 in &lt;code&gt;start-dev&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three diagnostics, in order
&lt;/h2&gt;

&lt;p&gt;Do these before changing any configuration. Together they name the cause in about a minute.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Read the Keycloak server log during one failing attempt.&lt;/strong&gt; A dropped cookie produces a specific event:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   WARN [org.keycloak.events] type="LOGIN_ERROR", realmId="369ce479-...",
   realmName="master", clientId="loopdemo", userId="null",
   ipAddress="172.17.0.1", error="cookie_not_found"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;error="cookie_not_found"&lt;/code&gt; means Keycloak received the request and found no auth session cookie on it. That is causes 1 through 4 below. No event at all means the browser is looping before it reaches Keycloak — that is cause 5.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Look at the URL the loop lands on.&lt;/strong&gt; If it carries &lt;code&gt;error=login_required&lt;/code&gt;, the loop is a silent SSO check, not a login:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   https://app.example.com/cb?error=login_required&amp;amp;state=xyz&amp;amp;iss=https%3A%2F%2Fauth.example.com%2Frealms%2Fmaster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Compare the host in the browser's address bar with the host in the login form's &lt;code&gt;action&lt;/code&gt; attribute.&lt;/strong&gt; If they differ, you have cause 3, and nothing else needs investigating.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What Keycloak actually sets
&lt;/h2&gt;

&lt;p&gt;Requesting the authorization endpoint over plain HTTP, with no proxy headers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="na"&gt;Set-Cookie: AUTH_SESSION_ID&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;snip&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;realms&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Secure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;HttpOnly&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;SameSite&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="na"&gt;Set-Cookie: KC_AUTH_SESSION_HASH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;snip&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;realms&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Secure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;SameSite&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="na"&gt;Set-Cookie: KC_RESTART&lt;/span&gt;&lt;span class="o"&gt;=&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;snip&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="o"&gt;=/&lt;/span&gt;&lt;span class="n"&gt;realms&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;Secure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;HttpOnly&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nc"&gt;SameSite&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After a successful password POST, &lt;code&gt;KEYCLOAK_IDENTITY&lt;/code&gt; and &lt;code&gt;KEYCLOAK_SESSION&lt;/code&gt; arrive with the same three attributes. Note the &lt;code&gt;Path&lt;/code&gt;: &lt;code&gt;/realms/&amp;lt;realm&amp;gt;/&lt;/code&gt;, not &lt;code&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One detail explains most "but it works on my laptop" reports. Browsers treat &lt;code&gt;localhost&lt;/code&gt;, &lt;code&gt;127.0.0.1&lt;/code&gt; and &lt;code&gt;[::1]&lt;/code&gt; as &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Security/Secure_contexts" rel="noopener noreferrer"&gt;potentially trustworthy origins&lt;/a&gt;, so they accept &lt;code&gt;Secure&lt;/code&gt; cookies there over plain HTTP. The same image on a real hostname over plain HTTP drops all five cookies. Local development cannot reproduce this class of bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 1: plain HTTP on a real hostname
&lt;/h2&gt;

&lt;p&gt;The browser silently discards every cookie above, posts the login form without &lt;code&gt;AUTH_SESSION_ID&lt;/code&gt;, and gets HTTP 400 with "Restart login cookie not found. It may have expired; it may have been deleted or cookies are disabled in your browser." Clicking back to the application restarts the flow, which is the loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; terminate TLS. There is no supported way to make Keycloak emit these cookies without &lt;code&gt;Secure&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 2: TLS at the proxy, &lt;code&gt;proxy-headers&lt;/code&gt; unset
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;proxy-headers&lt;/code&gt; has &lt;strong&gt;no default value&lt;/strong&gt;. If a proxy terminates TLS and you have not set it, Keycloak builds URLs and evaluates origins as if the request arrived on &lt;code&gt;http://&lt;/code&gt; at the backend address. The Keycloak docs are explicit about the consequence: "If you are using a reverse proxy for anything other than TLS passthrough and do not set the &lt;code&gt;proxy-headers&lt;/code&gt; option, then by default you will see 403 Forbidden responses to requests via the proxy that perform origin checking."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; set it to match what your proxy sends, and set the hostname explicitly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;KC_PROXY_HEADERS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;xforwarded     &lt;span class="c"&gt;# X-Forwarded-For/-Proto/-Host/-Port/-Prefix&lt;/span&gt;
&lt;span class="nv"&gt;KC_HOSTNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;https://auth.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;forwarded&lt;/code&gt; instead if your proxy emits the RFC 7239 &lt;code&gt;Forwarded&lt;/code&gt; header. Never set either with TLS passthrough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 3: the form posts to a host that never saw the cookie
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;hostname&lt;/code&gt; set to an absolute URL, Keycloak does not redirect requests that arrive elsewhere. It renders the login page on whatever host you reached, with the form action pointing at the configured one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# request arrives at http://10.0.1.7:8080/, KC_HOSTNAME=https://auth.example.com
action="https://auth.example.com/realms/master/login-actions/authenticate?session_code=..."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser stored &lt;code&gt;AUTH_SESSION_ID&lt;/code&gt; for the first host and posts to the second, which receives no cookie. That POST returns 400 and logs &lt;code&gt;error="cookie_not_found"&lt;/code&gt;. Health checks hitting the pod IP, a second ingress, and an internal DNS name used by one service all produce this.&lt;/p&gt;

&lt;p&gt;The mirror image is &lt;code&gt;hostname-strict=false&lt;/code&gt;, where the issuer floats with the request headers:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Request&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;issuer&lt;/code&gt; in the discovery document&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;X-Forwarded-Host: auth.example.com&lt;/code&gt;, &lt;code&gt;X-Forwarded-Proto: https&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;https://auth.example.com/realms/master&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;no forwarded headers&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://10.0.1.7:8080/realms/master&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two routes to the same server mint tokens with two issuers. Whichever one your application did not configure gets rejected, and a rejected token sends the user back to login.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; set &lt;code&gt;hostname&lt;/code&gt; to the exact absolute URL users reach, leave &lt;code&gt;hostname-strict&lt;/code&gt; at its default of &lt;code&gt;true&lt;/code&gt;, and make every other route to Keycloak fail rather than work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 4: a path prefix the proxy strips
&lt;/h2&gt;

&lt;p&gt;Serving Keycloak under &lt;code&gt;/auth&lt;/code&gt; and stripping the prefix before it reaches the server leaves the cookie scoped to the wrong path — the browser is at &lt;code&gt;/auth/realms/master/...&lt;/code&gt; and the cookie says &lt;code&gt;/realms/master/&lt;/code&gt;. Adding the header fixes the scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# no X-Forwarded-Prefix
Set-Cookie: AUTH_SESSION_ID=&amp;lt;snip&amp;gt;;Path=/realms/master/;Secure;HttpOnly;SameSite=None
# X-Forwarded-Prefix: /auth
Set-Cookie: AUTH_SESSION_ID=&amp;lt;snip&amp;gt;;Path=/auth/realms/master/;Secure;HttpOnly;SameSite=None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; send &lt;code&gt;X-Forwarded-Prefix&lt;/code&gt; from the proxy (and set &lt;code&gt;proxy-headers=xforwarded&lt;/code&gt;), or stop stripping the prefix and set &lt;code&gt;KC_HTTP_RELATIVE_PATH=/auth&lt;/code&gt;. Pick one; doing both double-prefixes every URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cause 5: the loop Keycloak never sees
&lt;/h2&gt;

&lt;p&gt;Two variants, neither of which logs anything on the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Silent check-SSO.&lt;/strong&gt; &lt;code&gt;keycloak-js&lt;/code&gt; with &lt;code&gt;onLoad: 'check-sso'&lt;/code&gt; sends &lt;code&gt;prompt=none&lt;/code&gt;. With the session cookies present Keycloak returns a code; without them it returns &lt;code&gt;error=login_required&lt;/code&gt; — and an app that treats that as "start a login" loops. Keycloak's JavaScript adapter documentation is direct about why the cookies go missing: silent &lt;code&gt;check-sso&lt;/code&gt; "is not supported and falls back to regular (non-silent) &lt;code&gt;check-sso&lt;/code&gt; by default" when the browser blocks cookies in third-party contexts, and the session status iframe "is automatically disabled if such browser behavior is detected". Safari blocks third-party cookies by default and Firefox partitions them, so this reproduces today regardless of what Chrome does. Handle &lt;code&gt;login_required&lt;/code&gt; as a terminal "not logged in" state, and keep &lt;code&gt;silentCheckSsoFallback&lt;/code&gt; at its default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your own session cookie.&lt;/strong&gt; With &lt;code&gt;response_mode=form_post&lt;/code&gt;, Keycloak returns an auto-submitting cross-site POST:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;BODY&lt;/span&gt; &lt;span class="na"&gt;Onload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"document.forms[0].submit()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;FORM&lt;/span&gt; &lt;span class="na"&gt;METHOD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;ACTION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://app.example.com/cb"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;SameSite=Lax&lt;/code&gt; application session cookie is sent on top-level GET navigations and &lt;strong&gt;not&lt;/strong&gt; on that POST. The framework sees no session, redirects to Keycloak, and loops. Either use &lt;code&gt;response_mode=query&lt;/code&gt;, or mark the application's session cookie &lt;code&gt;SameSite=None; Secure&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify the fix without guessing
&lt;/h2&gt;

&lt;p&gt;Keycloak ships the answer as a page. Set &lt;code&gt;KC_HOSTNAME_DEBUG=true&lt;/code&gt; and open &lt;code&gt;/realms/master/hostname-debug&lt;/code&gt; — it prints the frontend, backend and admin URLs the server will actually generate, next to the headers it received:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;URL       Value                     Header             Value
Frontend  https://auth.example.com/ Host               proxy.example.com
Backend   https://auth.example.com/ X-Forwarded-Host   proxy.example.com
Admin     https://auth.example.com/ X-Forwarded-Proto  https
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the three URLs are not what users type, stop and fix that first. Turn the option off afterwards — it is off by default for a reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop it recurring
&lt;/h2&gt;

&lt;p&gt;Pin the four values in your deployment manifest rather than discovering them again next quarter, and treat any route that does not go through the proxy as a bug:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="na"&gt;KC_HOSTNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//auth.example.com&lt;/span&gt;
&lt;span class="na"&gt;KC_HOSTNAME_STRICT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;KC_PROXY_HEADERS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;xforwarded&lt;/span&gt;
&lt;span class="na"&gt;KC_HTTP_ENABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;          &lt;span class="c1"&gt;# only because the proxy terminates TLS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then assert it. One CI check that fetches &lt;code&gt;/realms/&amp;lt;realm&amp;gt;/.well-known/openid-configuration&lt;/code&gt; through the public URL and asserts &lt;code&gt;issuer&lt;/code&gt; equals the configured hostname catches causes 2, 3 and 4 on the commit that introduces them. It is three lines of &lt;code&gt;curl&lt;/code&gt; and &lt;code&gt;jq&lt;/code&gt;, and it is the only one of these failures that is cheap to detect before users find it.&lt;/p&gt;




&lt;p&gt;Redirect loops are rarely hard once you know which of the five it is; they are expensive because nothing in the browser says "cookie discarded" and the server log entry is one line among thousands. If you would rather not own the proxy, the hostname settings and the TLS chain, Phase Two runs &lt;a href="https://phasetwo.io/hosting/dedicated-clusters/" rel="noopener noreferrer"&gt;managed Keycloak&lt;/a&gt; with a 30-day Starter trial from $149/month, terminated and configured correctly on day one. Staying self-hosted is the right call for plenty of teams: &lt;a href="https://phasetwo.io/blog/keycloak-invalid-grant" rel="noopener noreferrer"&gt;&lt;code&gt;invalid_grant&lt;/code&gt;&lt;/a&gt; covers the errors that appear once the loop clears, &lt;a href="https://phasetwo.io/blog/keycloak-invalid-redirect-uri" rel="noopener noreferrer"&gt;&lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;&lt;/a&gt; covers the other half of callback failures, the &lt;a href="https://phasetwo.io/blog/keycloak-production-checklist" rel="noopener noreferrer"&gt;production checklist&lt;/a&gt; has the rest of the proxy settings, and the &lt;a href="https://phasetwo.io/docs/securing-applications/javascript" rel="noopener noreferrer"&gt;JavaScript adapter docs&lt;/a&gt; cover &lt;code&gt;check-sso&lt;/code&gt; setup end to end.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://phasetwo.io/blog/keycloak-login-redirect-loop/" rel="noopener noreferrer"&gt;phasetwo.io&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was drafted with AI assistance and reviewed by the Phase Two team before publication.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>keycloak</category>
      <category>debugging</category>
      <category>oidc</category>
      <category>security</category>
    </item>
    <item>
      <title>Keycloak CORS errors: why your SPA gets blocked</title>
      <dc:creator>Garth</dc:creator>
      <pubDate>Thu, 03 Dec 2026 09:30:16 +0000</pubDate>
      <link>https://dev.to/xgp/keycloak-cors-errors-why-your-spa-gets-blocked-3685</link>
      <guid>https://dev.to/xgp/keycloak-cors-errors-why-your-spa-gets-blocked-3685</guid>
      <description>&lt;p&gt;A Keycloak CORS error is almost never a CORS-configuration problem in the general sense. There is no CORS server setting to turn on. Keycloak decides per request by comparing the browser's &lt;code&gt;Origin&lt;/code&gt; header against the &lt;strong&gt;Web Origins&lt;/strong&gt; list on the OIDC client named in that request, using exact string matching. If it matches, the response carries &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;. If it does not, you get &lt;code&gt;403&lt;/code&gt; with the body &lt;code&gt;{"error":"Invalid origin"}&lt;/code&gt; — and the browser reports it as a CORS failure, which sends most people to the wrong place.&lt;/p&gt;

&lt;p&gt;So the first thing to do is not to read a CORS guide. It is to open the Network tab, find the failing request, and check whether the status is 403 with that body. That single fact splits every cause below into two halves.&lt;/p&gt;

&lt;p&gt;Everything here was run against &lt;strong&gt;Keycloak 26.7.3&lt;/strong&gt; in &lt;code&gt;start-dev&lt;/code&gt;, with real &lt;code&gt;curl&lt;/code&gt; output.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 30-second fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Find the &lt;code&gt;client_id&lt;/code&gt; in the failing request's form body (or the &lt;code&gt;azp&lt;/code&gt; claim of the bearer token, if it is an API call).&lt;/li&gt;
&lt;li&gt;In the admin console, open &lt;strong&gt;Clients → that client → Settings → Web Origins&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Add the browser origin &lt;strong&gt;exactly&lt;/strong&gt;: scheme, host, port, no trailing slash, no path. &lt;code&gt;http://localhost:5173&lt;/code&gt;, not &lt;code&gt;http://localhost:5173/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Save. No restart, no cache flush — the next request picks it up.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the client is &lt;code&gt;admin-cli&lt;/code&gt;, stop: that one can never work from a browser. See &lt;a href="https://phasetwo.io/blog/keycloak-cors-errors/#the-admin-rest-api-needs-its-own-client" rel="noopener noreferrer"&gt;the admin API section&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your preflight is not the problem
&lt;/h2&gt;

&lt;p&gt;This is the measurement that surprises people. A CORS preflight carries no body, so Keycloak has no &lt;code&gt;client_id&lt;/code&gt; and cannot know which client's Web Origins to check. It therefore approves every preflight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s -i -X OPTIONS \
  http://localhost:8080/realms/cors-lab/protocol/openid-connect/token \
  -H &lt;span class="s2"&gt;"Origin: http://totally-unrelated.example"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Access-Control-Request-Method: POST"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Access-Control-Request-Headers: content-type"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="no"&gt;OK&lt;/span&gt;
&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//totally-unrelated.example&lt;/span&gt;
&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;OPTIONS&lt;/span&gt;
&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Max&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The origin check happens on the &lt;strong&gt;actual&lt;/strong&gt; request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s -i -X POST \
  http://localhost:8080/realms/cors-lab/protocol/openid-connect/token \
  -H &lt;span class="s2"&gt;"Origin: http://localhost:3000"&lt;/span&gt; \
  -d &lt;span class="nv"&gt;client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;spa-exact -d &lt;span class="nv"&gt;grant_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;authorization_code -d &lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;403&lt;/span&gt; &lt;span class="nc"&gt;Forbidden&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Invalid origin"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The code is fake, so an allowed origin returns &lt;code&gt;400 invalid_grant&lt;/code&gt; here — the presence or&lt;br&gt;
absence of the CORS headers is the whole signal.)&lt;/p&gt;

&lt;p&gt;So if your browser console says &lt;em&gt;"Response to preflight request doesn't pass access control check"&lt;/em&gt;, the preflight genuinely failed — which means something in front of Keycloak answered it, not Keycloak. If the console says &lt;em&gt;"No 'Access-Control-Allow-Origin' header is present"&lt;/em&gt; on the POST itself, it is the Web Origins list or a proxy.&lt;/p&gt;

&lt;p&gt;Note also that &lt;code&gt;403 {"error":"Invalid origin"}&lt;/code&gt; never reaches your JavaScript. The browser strips the response before your &lt;code&gt;catch&lt;/code&gt; block sees it. You will only find it in the Network tab or in &lt;code&gt;docker logs&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The matching rules, measured
&lt;/h2&gt;

&lt;p&gt;Every row below is one run against 26.7.3: set Web Origins, send the origin, record the status.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Web Origins entry&lt;/th&gt;
&lt;th&gt;Browser &lt;code&gt;Origin&lt;/code&gt;
&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;200&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173/app&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;HTTP://LOCALHOST:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://app.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://app.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://app.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;https://app.example.com:443&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://*.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;https://a.example.com&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://localhost&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://localhost:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;http://127.0.0.1:5173&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;403&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;200&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Byte-for-byte string comparison. No wildcards, no scheme normalization, no default-port folding, no case folding, and &lt;code&gt;127.0.0.1&lt;/code&gt; is not &lt;code&gt;localhost&lt;/code&gt;. The trailing slash is the single most common cause — the admin console will happily accept &lt;code&gt;http://localhost:5173/&lt;/code&gt; because it accepts any string.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Two special values, and they are not symmetric:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;th&gt;Means&lt;/th&gt;
&lt;th&gt;Use when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allow the origins of this client's &lt;strong&gt;Valid redirect URIs&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Almost always. One list to maintain.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allow any origin&lt;/td&gt;
&lt;td&gt;Local experiments only.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;(empty)&lt;/em&gt;&lt;/td&gt;
&lt;td&gt;Allow nothing&lt;/td&gt;
&lt;td&gt;Server-to-server clients. Correct default.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;+&lt;/code&gt; derives origins from absolute redirect URIs, so &lt;code&gt;https://app.example.com/callback&lt;/code&gt; yields &lt;code&gt;https://app.example.com&lt;/code&gt;. Verified limits: a wildcard &lt;strong&gt;host&lt;/strong&gt; in a redirect URI (&lt;code&gt;https://*.example.com/callback&lt;/code&gt;) contributes nothing, and a relative redirect URI (&lt;code&gt;/callback/*&lt;/code&gt;) only contributes if the client has a &lt;strong&gt;Root URL&lt;/strong&gt; set, in which case the origin comes from that. You can mix — &lt;code&gt;["+", "https://extra.example.com"]&lt;/code&gt; allows both sets.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;*&lt;/code&gt; deserves a warning that is usually stated backwards. The common claim is that &lt;code&gt;*&lt;/code&gt; breaks credentialed requests. On 26.7.3 it does not: Keycloak &lt;strong&gt;reflects&lt;/strong&gt; the caller's origin rather than emitting a literal &lt;code&gt;*&lt;/code&gt;, and still sends &lt;code&gt;Access-Control-Allow-Credentials: true&lt;/code&gt;. That is worse, not better. It also means &lt;code&gt;Origin: null&lt;/code&gt; — sandboxed iframes, &lt;code&gt;data:&lt;/code&gt; documents, some redirect chains — gets &lt;code&gt;Access-Control-Allow-Origin: null&lt;/code&gt; and a credentialed response. Do not ship &lt;code&gt;*&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The admin REST API needs its own client
&lt;/h2&gt;

&lt;p&gt;The Admin REST API applies the same rule, against the Web Origins of the client that &lt;strong&gt;issued the bearer token&lt;/strong&gt;. The trap is that the obvious client to reach for is the wrong one. In a fresh realm:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Built-in client&lt;/th&gt;
&lt;th&gt;Web Origins&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;admin-cli&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;em&gt;(empty)&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;account&lt;/code&gt;, &lt;code&gt;account-console&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;em&gt;(empty)&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;security-admin-console&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;admin-cli&lt;/code&gt; is the client every tutorial uses for &lt;code&gt;kcadm.sh&lt;/code&gt;, and it has no Web Origins, so a browser app that authenticates with it is rejected before it ever reaches an admin endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="na"&gt;POST /realms/cors-lab/protocol/openid-connect/token  (client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;cli&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;403&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"Invalid origin"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create your own public client for the admin SPA, give it the realm-management roles it needs, and set its Web Origins. Then it works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="na"&gt;GET /admin/realms/cors-lab/users?max&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//localhost:5173&lt;/span&gt;
→ &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//localhost:5173&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing worth knowing, because the official docs imply otherwise: the check on token-bearing endpoints uses the client's &lt;strong&gt;current&lt;/strong&gt; configuration, not the &lt;code&gt;allowed-origins&lt;/code&gt; claim baked into the token. We confirmed this by removing an origin from the client and replaying a token that still listed it in its claim — the request went from 200 to 403. Fixing Web Origins takes effect immediately; you do not need users to re-authenticate.&lt;/p&gt;

&lt;h2&gt;
  
  
  When it is the proxy, not Keycloak
&lt;/h2&gt;

&lt;p&gt;Two failures that no amount of Web Origins editing will fix. Both were reproduced with nginx in front of Keycloak.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The proxy strips &lt;code&gt;Origin&lt;/code&gt;.&lt;/strong&gt; A &lt;code&gt;proxy_set_header Origin "";&lt;/code&gt; anywhere in the chain, or a gateway that normalizes headers, and Keycloak sees a same-origin request. It returns &lt;code&gt;200&lt;/code&gt; and &lt;em&gt;no&lt;/em&gt; &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; at all, so the browser blocks a perfectly successful response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="no"&gt;OK&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt; &lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-*&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The proxy adds its own &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;.&lt;/strong&gt; Someone hit a CORS error, added &lt;code&gt;add_header Access-Control-Allow-Origin "*" always;&lt;/code&gt; to the nginx config, and now every response carries two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//localhost:5173&lt;/span&gt;
&lt;span class="nc"&gt;Access&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Control&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Allow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Chrome rejects this outright — &lt;em&gt;"the 'Access-Control-Allow-Origin' header contains multiple values ... but only one is allowed"&lt;/em&gt; — so the well-meant fix breaks the requests that previously worked. Keycloak sets its own CORS headers on every OIDC endpoint. Your proxy must set none.&lt;/p&gt;

&lt;p&gt;A smaller one: Keycloak does not send &lt;code&gt;Vary: Origin&lt;/code&gt;. The endpoints that matter carry &lt;code&gt;Cache-Control: no-store&lt;/code&gt; or &lt;code&gt;no-cache&lt;/code&gt;, so a well-behaved CDN will not cache one origin's headers and serve them to another — but a CDN configured to override cache headers will, and the symptom is a CORS error that only some users see.&lt;/p&gt;

&lt;h2&gt;
  
  
  When it is not CORS at all
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The login redirect.&lt;/strong&gt; &lt;code&gt;/protocol/openid-connect/auth&lt;/code&gt; sends no CORS headers and never has. It is a top-level navigation, not a fetch. If you see a CORS error there, your code is calling the authorization endpoint with &lt;code&gt;fetch()&lt;/code&gt; instead of redirecting the browser. Use the redirect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discovery and JWKS work from any origin.&lt;/strong&gt; &lt;code&gt;/.well-known/openid-configuration&lt;/code&gt; and &lt;code&gt;/protocol/openid-connect/certs&lt;/code&gt; answer every origin, by design. "Discovery loads but the token call fails" is the normal shape of this bug, not a clue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A 400 or 401 that you cannot read.&lt;/strong&gt; Once the origin is allowed, the browser hands you the response — and it often turns out the real error was &lt;a href="https://phasetwo.io/blog/keycloak-invalid-grant" rel="noopener noreferrer"&gt;&lt;code&gt;invalid_grant&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://phasetwo.io/blog/keycloak-invalid-redirect-uri" rel="noopener noreferrer"&gt;&lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;&lt;/a&gt; all along. CORS was hiding it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Reproduce the whole thing in a minute
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run -d --name kc -p 127.0.0.1:8080:8080 \
  -e &lt;span class="nv"&gt;KC_BOOTSTRAP_ADMIN_USERNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;admin -e &lt;span class="nv"&gt;KC_BOOTSTRAP_ADMIN_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;admin \
  quay.io/keycloak/keycloak:26.7.3 start-dev

docker &lt;span class="nb"&gt;exec&lt;/span&gt; kc /opt/keycloak/bin/kcadm.sh config credentials \
  --server http://localhost:8080 --realm master --user admin --password admin

docker &lt;span class="nb"&gt;exec&lt;/span&gt; kc /opt/keycloak/bin/kcadm.sh create clients -r master \
  -s &lt;span class="nv"&gt;clientId&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;spa-exact -s &lt;span class="nv"&gt;publicClient&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; \
  -s &lt;span class="s1"&gt;'redirectUris=["http://localhost:5173/*"]'&lt;/span&gt; \
  -s &lt;span class="s1"&gt;'webOrigins=["http://localhost:5173"]'&lt;/span&gt;

&lt;span class="c"&gt;# allowed origin → 400 invalid_grant, but with CORS headers&lt;/span&gt;
&lt;span class="c"&gt;# change 5173 to 3000  → 403 {"error":"Invalid origin"}, no CORS headers&lt;/span&gt;
curl -s -i -X POST \
  http://localhost:8080/realms/master/protocol/openid-connect/token \
  -H &lt;span class="s2"&gt;"Origin: http://localhost:5173"&lt;/span&gt; \
  -d &lt;span class="nv"&gt;client_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;spa-exact -d &lt;span class="nv"&gt;grant_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;authorization_code -d &lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;x \
  &lt;span class="o"&gt;|&lt;/span&gt; head -6

docker rm -f kc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ten seconds of setup beats an afternoon of guessing which layer is lying to you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the official docs cover
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.keycloak.org/docs/latest/server_admin/index.html#_oidc_clients" rel="noopener noreferrer"&gt;Server Administration Guide&lt;/a&gt; describes the Web Origins field, and it is worth reading for the security rationale. Two gaps to be aware of: it documents the field mainly in terms of the &lt;code&gt;allowed-origins&lt;/code&gt; claim being embedded in the token for &lt;em&gt;client adapters&lt;/em&gt; to enforce — which describes the retired Java adapters, not what a modern Keycloak server does on its own endpoints — and it does not cover &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, the matching rules, or the &lt;code&gt;Invalid origin&lt;/code&gt; response. The tables above are the part that is missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop it recurring
&lt;/h2&gt;

&lt;p&gt;Put Web Origins in your realm export or Terraform, alongside redirect URIs, and use &lt;code&gt;+&lt;/code&gt; so there is one list to get wrong instead of two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"clientId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"spa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"publicClient"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"redirectUris"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"https://app.example.com/*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"webOrigins"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"+"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"attributes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"post.logout.redirect.uris"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://app.example.com/*"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add one line to your smoke tests: a &lt;code&gt;POST&lt;/code&gt; to the token endpoint with the production &lt;code&gt;Origin&lt;/code&gt; header, asserting the response carries &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;. It catches the trailing slash before your users do.&lt;/p&gt;




&lt;p&gt;Most CORS reports are a missing string in one field, and the expensive part is the hour spent proving which of the browser, the proxy and Keycloak is at fault. If you would rather not spend it, Phase Two runs &lt;a href="https://phasetwo.io/hosting/dedicated-clusters/" rel="noopener noreferrer"&gt;managed Keycloak&lt;/a&gt; with a 30-day Starter trial from $149/month. If you are staying self-hosted, the &lt;a href="https://phasetwo.io/tutorials/" rel="noopener noreferrer"&gt;Keycloak tutorials&lt;/a&gt; — the &lt;a href="https://phasetwo.io/tutorials/getting-started/your-first-token/" rel="noopener noreferrer"&gt;first token walkthrough&lt;/a&gt; covers the &lt;code&gt;allowed-origins&lt;/code&gt; claim — and the &lt;a href="https://phasetwo.io/docs/securing-applications/javascript" rel="noopener noreferrer"&gt;securing applications docs&lt;/a&gt; cover the browser side of this.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://phasetwo.io/blog/keycloak-cors-errors/" rel="noopener noreferrer"&gt;phasetwo.io&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was drafted with AI assistance and reviewed by the Phase Two team before publication.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>keycloak</category>
      <category>debugging</category>
      <category>cors</category>
      <category>oidc</category>
    </item>
  </channel>
</rss>
