Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Spring Security rejects the Entra ID token: invalid claims

ErrorError occurred while attempting to decode the Jwt. The ID Token contains invalid claims

Short answer

Spring checks the ID token's claims against your client registration. With Entra the failure is nearly always the issuer: the token's iss does not equal the issuer from your configured metadata, because of v1.0 versus v2.0 or a common tenant. Next suspects are the audience and the clock.

  • Applies to Spring Boot 3, Spring Security 6, Microsoft Entra ID
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Decode the ID token your app received (a test sign-in is fine) and compare its iss and aud claims with your configuration.

    echo "$ID_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1iss starts with https://sts.windows.net/ or contains a tenant id that is not the one in issuer-uriiss in the token differs from the issuer in your configured metadataFix
    2. 2aud in the decoded token is a different application's IDaud does not match your client IDFix
    3. 3iat, nbf or exp is outside the allowed time, or the nonce differsThe nonce or the timestamps fail validationFix

What does the error look like?

  • Error occurred while attempting to decode the Jwt. The ID Token contains invalid claims

Cause 1 of 3

iss in the token differs from the issuer in your configured metadata

1Check

The ID token version follows the endpoint it came from: the oauth2/authorize endpoint gives v1.0, oauth2/v2.0/authorize gives v2.0. The v2.0 issuer has the form https://login.microsoftonline.com/{tenantid}/v2.0, and a token must carry exactly the issuer of the metadata used to validate it. A metadata path with common returns a template, not a real tenant issuer.

grep -rn "issuer-uri\|authorization-uri" src/main/resources/

2Fix

Use the v2.0 endpoints with your real tenant id.

spring:
  security:
    oauth2:
      client:
        provider:
          azure:
            issuer-uri: https://login.microsoftonline.com/<tenant-id>/v2.0

3Verify

Sign in again; the redirect completes and a session is created.

curl -s https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration | grep '"issuer"'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

aud does not match your client ID

1Check

An ID token must carry your own application's ID as audience. A mismatch usually means the client-id in the properties belongs to another app registration.

grep -rn "client-id" src/main/resources/

2Fix

Set client-id to the Application (client) ID of the registration that is doing the sign-in.

spring:
  security:
    oauth2:
      client:
        registration:
          azure:
            client-id: <application-client-id>

3Verify

Decode a new ID token: aud equals the configured client-id.

echo "$ID_TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | grep -o '"aud":"[^"]*"'

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The nonce or the timestamps fail validation

1Check

iat, nbf and exp must fall before or after the current time as appropriate, and the nonce must be the one sent in the authorize request. A wrong server clock breaks the timestamps.

timedatectl status | grep -i 'synchronized'

2Fix

Sync the server clock (NTP) and start the sign-in from a fresh page.

sudo timedatectl set-ntp true

3Verify

The clock reports synchronized, and a new sign-in works.

timedatectl status | grep -i 'synchronized'

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep the tenant id in the issuer-uri; avoid common for ID-token validation.
  • Keep one app registration per environment and one client-id per Spring profile.
  • Run NTP on every server that validates tokens.

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.