Skip to content

Blog

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 iss claim, 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-Authenticate header carrying error="invalid_token" and error_description="Token verification failed". With saved events turned on, a USER_INFO_REQUEST_ERROR event records the error invalid_token and 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:

  1. The realm's Frontend URL setting, if one is set (Realm settings, General).
  2. The server's hostname option. With a full URL such as https://auth.example.com, scheme, host, port and path are fixed.
  3. The request itself, when hostname is only a host name or hostname-strict is 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

  1. The app fetched discovery or tokens through an internal URL while Keycloak has no fixed hostname. Tokens issued through http://keycloak:8080 say http://keycloak:8080/realms/acme; the app expects the public URL, or the other way around.
  2. The app's configured issuer has the old /auth path. The configured value has /auth/realms/acme; tokens from Keycloak 17 and later have /realms/acme unless you set a path.
  3. http and https differ. A proxy does not send X-Forwarded-Proto, so Keycloak builds http:// URLs, while the app expects https://.
  4. A port appears on one side. One value has :8443 or :443 and the other has none.
  5. A trailing slash. The app is configured with https://auth.example.com/realms/acme/. Keycloak's issuer has no trailing slash.
  6. The realm name differs in case. The app says Acme, the realm is acme. The issuer uses the realm name exactly as stored.
  7. A Frontend URL is set on one realm and the app's config was written from the server's hostname.
  8. The hostname changed after tokens were issued, or the app cached an old discovery document.

Diagnose it step by step

  1. Decode a failing token and copy the iss claim exactly.

  2. Read the issuer the app expects. Find it in the app's config, or in its error message.

  3. 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
    
  4. 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-configuration
    

    If the two issuers differ, Keycloak is resolving its address from requests. Fix the hostname, not the app.

  5. Compare the three strings character by character: scheme, host, port, path, realm name, trailing slash.

  6. Check the server options with bin/kc.sh show-config. Note hostname, hostname-strict, hostname-backchannel-dynamic and proxy-headers.

  7. Check the realm's Frontend URL in the admin console.

  8. Look at Keycloak's events. A USER_INFO_REQUEST_ERROR with the reason Invalid token issuer. Expected '...' shows the URL Keycloak expected for that request.

  9. Use the hostname debug page for a short time: start with --hostname-debug=true and open /realms/<realm>/hostname-debug through 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 iss claim of a failing token.
  • JWT claims validator: check iss, aud and 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 hostname and hostname-backchannel-dynamic options, 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.