Keycloak invalid token issuer: fixing an iss mismatch
Why apps reject Keycloak tokens with an invalid issuer error: internal and public URLs, hostname options, Frontend URL, the /auth path, and library settings.
- Author
- The RealmDeploy team
- Published
Every token Keycloak issues carries an iss claim: the address of the realm that issued it. Every app that checks tokens compares that claim with the issuer it expects. If the two strings differ in any way, the app rejects the token.
That is the whole error. The hard part is that the two strings are built in different places. Keycloak builds iss from its own idea of its public address. Your app's expected issuer comes from a config file, an environment variable, or a discovery document fetched from some URL. When those disagree, sign-in seems to work and then every API call fails.
This post explains how Keycloak 26.x builds the issuer, the usual reasons it does not match, and how to fix each one.
What you see
The message depends on what does the checking.
-
Your app or API gateway rejects the token with its own wording. Libraries usually say the issuer, or the
issclaim, is invalid, and many print the expected and actual values. The HTTP status is typically 401. -
Keycloak itself checks the issuer when a token comes back to it, for example on the userinfo endpoint. In Keycloak 26.8.2 the check fails with this message:
Invalid token issuer. Expected '<realm URL>'The userinfo endpoint then answers 401 with a
WWW-Authenticateheader carryingerror="invalid_token"anderror_description="Token verification failed". With saved events turned on, aUSER_INFO_REQUEST_ERRORevent records the errorinvalid_tokenand the message above as its reason.
How Keycloak builds the issuer
The issuer is the realm's frontend base URL followed by /realms/ and the realm name:
https://auth.example.com/realms/acme
The frontend base URL comes from, in order:
- The realm's Frontend URL setting, if one is set (Realm settings, General).
- The server's
hostnameoption. With a full URL such ashttps://auth.example.com, scheme, host, port and path are fixed. - The request itself, when
hostnameis only a host name orhostname-strictis false: scheme, host, port and path come from the request or from the proxy's forwarded headers.
One detail removes a lot of confusion. With hostname-backchannel-dynamic=true, apps can call the token endpoint on an internal address, and Keycloak resolves the backchannel URLs from that request. The issuer does not change: Keycloak builds iss from the frontend URL, so it stays the public address. That option exists so internal calls work without breaking the issuer.
Causes, most common first
- The app fetched discovery or tokens through an internal URL while Keycloak has no fixed hostname. Tokens issued through
http://keycloak:8080sayhttp://keycloak:8080/realms/acme; the app expects the public URL, or the other way around. - The app's configured issuer has the old
/authpath. The configured value has/auth/realms/acme; tokens from Keycloak 17 and later have/realms/acmeunless you set a path. - http and https differ. A proxy does not send
X-Forwarded-Proto, so Keycloak buildshttp://URLs, while the app expectshttps://. - A port appears on one side. One value has
:8443or:443and the other has none. - A trailing slash. The app is configured with
https://auth.example.com/realms/acme/. Keycloak's issuer has no trailing slash. - The realm name differs in case. The app says
Acme, the realm isacme. The issuer uses the realm name exactly as stored. - A Frontend URL is set on one realm and the app's config was written from the server's hostname.
- The hostname changed after tokens were issued, or the app cached an old discovery document.
Diagnose it step by step
-
Decode a failing token and copy the
issclaim exactly. -
Read the issuer the app expects. Find it in the app's config, or in its error message.
-
Open the discovery document through the public URL and read
issuer:curl -s https://auth.example.com/realms/acme/.well-known/openid-configuration | jq -r .issuer -
Open it again through the URL the app uses, for example from inside the cluster:
kubectl exec deploy/my-api -- \ curl -s http://keycloak:8080/realms/acme/.well-known/openid-configurationIf the two issuers differ, Keycloak is resolving its address from requests. Fix the hostname, not the app.
-
Compare the three strings character by character: scheme, host, port, path, realm name, trailing slash.
-
Check the server options with
bin/kc.sh show-config. Notehostname,hostname-strict,hostname-backchannel-dynamicandproxy-headers. -
Check the realm's Frontend URL in the admin console.
-
Look at Keycloak's events. A
USER_INFO_REQUEST_ERRORwith the reasonInvalid token issuer. Expected '...'shows the URL Keycloak expected for that request. -
Use the hostname debug page for a short time: start with
--hostname-debug=trueand open/realms/<realm>/hostname-debugthrough each path. It shows how each URL was resolved.
Fixes
Set one public hostname as a full URL
Give Keycloak a fixed public address, so the issuer does not depend on who asks:
bin/kc.sh start \
--hostname=https://auth.example.com \
--http-enabled=true \
--proxy-headers=xforwarded
The issuer is now https://auth.example.com/realms/<realm> for every token, whichever way the request arrived.
Let internal calls work without changing the issuer
If apps inside your network call Keycloak on an internal address, add hostname-backchannel-dynamic. It needs hostname to be a full URL:
bin/kc.sh start \
--hostname=https://auth.example.com \
--hostname-backchannel-dynamic=true \
--http-enabled=true \
--proxy-headers=xforwarded
Tokens keep the public issuer. In the app, configure the public issuer for validation, even if the app fetches keys or tokens over the internal address.
Configure the app with the exact issuer
Copy issuer from the public discovery document into the app. Do not type it by hand. Some common settings:
# Spring Boot resource server
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://auth.example.com/realms/acme
// Node.js with the jose library
import { createRemoteJWKSet, jwtVerify } from 'jose';
const issuer = 'https://auth.example.com/realms/acme';
const jwks = createRemoteJWKSet(new URL(`${issuer}/protocol/openid-connect/certs`));
const { payload } = await jwtVerify(token, jwks, { issuer, audience: 'my-api' });
Spring's issuer-uri fetches discovery from that URL, so the app must be able to reach the public address. If it cannot, configure the JWKS URL for key download and keep the issuer check on the public value. Do not turn the issuer check off. It is what stops a token from another realm or another server being accepted.
Remove or keep the /auth path, on purpose
Keycloak 17 and later serve at /. If you upgraded from an older version, either update every app to drop /auth, or keep the path on the server for now:
bin/kc.sh start --hostname=https://auth.example.com/auth --http-relative-path=/auth
Pick one and move all apps to it. Tokens issued under one path do not validate against the other.
Fix the scheme behind a proxy
If tokens say http://, the proxy is not telling Keycloak that the user came over HTTPS, and hostname is not a full https:// URL. Set hostname as a full URL, and have the proxy send X-Forwarded-Proto: https. See 502 errors and redirect loops behind a reverse proxy.
Fix trailing slashes and realm names
Use the issuer exactly as discovery prints it: no trailing slash, and the realm name in the same case.
Use Frontend URL only when one realm needs a different address
A realm's Frontend URL overrides the server's hostname for that realm's frontend URLs, including its issuer. It is useful when one realm is served on a different host. If it was set by mistake, empty the field in Realm settings, General, and save. The realm then uses the server's hostname again. To see the value from the command line:
kcadm.sh get realms/acme --fields attributes
After a hostname change
Changing the public address changes the issuer of every realm. Old tokens stop validating, and apps must be updated with the new issuer. Plan it like a migration: update apps first where they can accept both values, then switch.
On RealmDeploy
Your issuer is fixed by us. Each tenant's Keycloak has hostname set to https:// plus your own hostname, so every token says https://<your hostname>/realms/<realm>, whichever way the request arrives. Copy the issuer from your realm's discovery document into your apps. The realm's Frontend URL is a realm setting in your hands: leave it empty, because your tenant answers on one hostname and a different value would change that realm's issuer.
Choose the hostname before setup and keep it. It is part of every realm's issuer, so changing it later means changing every app and identity provider that uses it. See Custom domains and TLS.
Tools that help
- JWT decoder: read the
issclaim of a failing token. - JWT claims validator: check
iss,audand the time claims against the values your app expects. - JWT verifier: verify a token's signature against the realm's keys.
- OIDC discovery viewer: read the issuer and endpoints from a discovery document.
- Hostname helper: build
hostnameandhostname-backchannel-dynamicoptions, and see the resulting issuer. - JWKS viewer: check that the app downloads keys from the right realm.
- Realm endpoints: list the issuer, discovery and key URLs for a realm.
Keep reading
Keycloak CORS errors: causes and fixes
Why the browser blocks calls to Keycloak with a CORS error, how Web origins work, how to find the real status code behind it, and how to fix each cause.
Keycloak "HTTPS required": causes and safe fixes
What the Keycloak "HTTPS required" page means, how Require SSL decides, how to get back into a locked master realm, and why turning SSL off is the wrong fix.
Keycloak invalid_grant: causes and fixes
Why the Keycloak token endpoint answers invalid_grant: reused or expired codes, redirect_uri mismatches, ended sessions, stale refresh tokens and PKCE.