spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

Azure AD Multi-Tenant OIDC Discovery Fails Due to '{tenantid}' in Issuer URI

Open Dyskal opened this issue 1 year ago • 5 comments

Describe the bug When configuring Spring Security's OAuth2 client for multi-tenant Azure Active Directory (Microsoft Entra ID) by setting the spring.security.oauth2.client.provider.<id>.issuer-uri to https://login.microsoftonline.com/common/v2.0 or https://login.microsoftonline.com/organizations/v2.0 and relying on issuer auto-discovery, the application fails to start with an UnsatisfiedDependencyException and a URISyntaxException.

The root cause is that the OpenID Connect discovery document fetched from these multi-tenant Azure AD endpoints contains an issuer field with a {tenantid} placeholder (e.g., "issuer": "https://login.microsoftonline.com/{tenantid}/v2.0"). The underlying URI parser considers {tenantid} an "Illegal character in path," leading to the URISyntaxException during the ClientRegistrations.parse step.

This prevents proper multi-tenant configuration through simple issuer-uri auto-discovery as per Spring Security's standard OIDC client configuration.

To Reproduce Steps to reproduce the behavior:

  1. Create a Spring Boot 3.5.x application with spring-boot-starter-oauth2-client and spring-boot-starter-security.

  2. Configure application.yml with Azure AD (Entra ID) client details, specifically setting the issuer-uri to the multi-tenant common endpoint:

spring:
  security:
    oauth2:
      client:
        registration:
          entra:
            client-id: <YOUR_AZURE_AD_CLIENT_ID>
            client-secret: <YOUR_AZURE_AD_CLIENT_SECRET>
            redirect-uri: "{baseUrl}/oauth2/code/{registrationId}"
            authorization-grant-type: authorization_code
            scope:
              - openid
              - profile
              - email
              - User.Read
        provider:
          entra:
            issuer-uri: https://login.microsoftonline.com/common/v2.0 # Or https://login.microsoftonline.com/organizations/v2.0
  1. Run the Spring Boot application.

Expected behavior The application should start successfully, and Spring Security should be able to resolve the OIDC configuration for multi-tenant Azure AD, allowing users from different Azure AD organizations to authenticate. The {tenantid} placeholder in the discovered issuer should be handled gracefully, allowing for multi-tenant applications.

Sample A minimal sample involve an empty Spring Boot application with the application.yml configuration as described. Such reproduction is created here : https://github.com/Dyskal/azure-oauth2-mre

Dyskal avatar Sep 22 '25 15:09 Dyskal

Can i contribute to this?

builtDifferentCoder avatar Sep 23 '25 19:09 builtDifferentCoder

what did you think about that?

builtDifferentCoder avatar Sep 27 '25 12:09 builtDifferentCoder

Same issue here in SpringBoot4 sadly. Any plan to resolve this ? or a walkaroud

jncanches avatar Jan 22 '26 12:01 jncanches

@Dyskal Please take a look at 4.3. OpenID Provider Configuration Validation:

The issuer value returned MUST be identical to the Issuer URL that was used as the prefix to /.well-known/openid-configuration to retrieve the configuration information. This MUST also be identical to the iss Claim value in ID Tokens issued from this Issuer.

Azure's implementation is non-compliant.

Requesting https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration returns

"issuer": "https://login.microsoftonline.com/{tenantid}/v2.0", which is not the same as https://login.microsoftonline.com/common/v2.0

I don't believe any changes are needed on our end.

jgrandja avatar Jan 22 '26 22:01 jgrandja

For anyone looking for the workaround @jncanches asked about: the Entra multi-tenant endpoints publish an issuer template on purpose. Fetched today:

common/v2.0                                -> https://login.microsoftonline.com/{tenantid}/v2.0
organizations/v2.0                         -> https://login.microsoftonline.com/{tenantid}/v2.0
72f988bf-86f1-41af-91ab-2d7cd011db47/v2.0  -> https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/v2.0

A token is always issued by the user's home tenant, so /common can't name a single issuer. Microsoft's own .NET validator resolves the template per token: it takes the tid claim and requires iss to equal the template with {tenantid} replaced by it (AadIssuerValidator.IsValidIssuer). In Spring that gives two options:

  1. Single tenant. Use the tenant-specific issuer, https://login.microsoftonline.com/<tenant-id>/v2.0, as issuer-uri. Discovery and the exact iss check both work unchanged.
  2. Multi-tenant. Leave out issuer-uri and set authorization-uri, token-uri and jwk-set-uri on the provider explicitly. There is a catch: with no issuer URI, OidcIdTokenValidator skips the iss check completely (OidcIdTokenValidator.java#L78-L82: if (metadataIssuer != null && ...)). A token from any Entra tenant would then pass. Add the check yourself:
    • Register an OidcIdTokenDecoderFactory bean and call setJwtValidatorFactory.
    • Have the factory return JwtValidators.createDefaultWithValidators(new OidcIdTokenValidator(registration), entraIssuerValidator). That keeps the default exp/nbf and aud/azp checks, because the factory you set replaces the default one, which builds exactly that minus your validator (DefaultOidcIdTokenValidatorFactory.java#L35-L37).
    • entraIssuerValidator should require iss to equal "https://login.microsoftonline.com/" + tid + "/v2.0". If only some customer tenants may sign in, it should also check tid against an allow-list.

basitalisandhu avatar Oct 26 '26 09:10 basitalisandhu