Sub topic 7
Configuring the JwtDecoder
This chapter assumes you know basic Spring Boot configuration, can read a JWT's claims, and understand that a resource server verifies a token's signature with the issuer's public keys. Here you will see how Spring Security builds a JwtDecoder, which property to choose, and how to replace the decoder with your own.
What the JwtDecoder does
A JwtDecoder is the component that turns the raw bearer string into a trusted Jwt object. It parses the token, verifies the signature against a key, and runs a set of validators. If any step fails, the request is rejected with a 401 response.
Spring Boot creates one for you when the right properties are present. You only write your own bean when you need extra checks.
Choosing a property
Two properties configure the decoder. They differ in how the keys are found.
| Property | What Boot does | When to use it |
|---|---|---|
| `issuer-uri` | Calls the provider's metadata endpoint, reads `jwks_uri`, and validates the `iss` claim | The provider publishes OpenID Connect or OAuth2 metadata |
| `jwk-set-uri` | Fetches keys directly from the URL you give | The provider has no metadata endpoint, or you want to skip discovery |
With jwk-set-uri alone, the iss claim is not checked unless you add a validator yourself.
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer.example.com/realms/demoSetting issuer-uri lets Spring Boot discover the provider's metadata and JWK set, and validate the iss claim of incoming tokens.
A discovery gotcha
With issuer-uri, Boot contacts the provider when the decoder is first used, not only at startup. Newer Boot versions resolve the metadata lazily, so a provider outage does not always stop the application from starting. The first request then fails instead. The issuer-uri value must also match the iss claim exactly, including any trailing slash.
Writing your own decoder
Declare a JwtDecoder bean when you need to add validators, such as an audience check. Spring Boot backs off its auto-configured decoder when it finds yours.
@Bean
JwtDecoder jwtDecoder(
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}") String issuer) {
NimbusJwtDecoder decoder = NimbusJwtDecoder.withIssuerLocation(issuer).build();
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuer);
OAuth2TokenValidator<Jwt> withAudience = new JwtClaimValidator<List<String>>(
"aud", aud -> aud != null && aud.contains("orders-api"));
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(withIssuer, withAudience));
return decoder;
}Step by step:
NimbusJwtDecoder.withIssuerLocation(issuer)builds a decoder that discovers the JWK set from the issuer's metadata.JwtValidators.createDefaultWithIssuer(issuer)keeps the default timestamp checks (exp,nbf) and adds theisscheck.JwtClaimValidatoradds the audience rule. It rejects any token whoseaudclaim does not containorders-api.DelegatingOAuth2TokenValidatorcombines the validators. A token must pass all of them.
Common errors
- Calling
setJwtValidatorwith only the audience validator. This replaces the defaults, so expiry and issuer are no longer checked. Always combine your validator with the default ones. - Mismatched issuer. A trailing slash or an
httpversushttpsdifference causes every token to fail with an invalidiss. - Using
jwk-set-uriand assuming the issuer is verified. It is not, unless you add the validator. - Wrong
audtype. Some providers sendaudas a single string. Spring'sJwtobject exposes it as a list, so the check above works, but a custom parser may not.
Exercise
A resource server sets only jwk-set-uri. A token signed by the correct key but issued by a different issuer arrives. Is it accepted, and how would you fix this?
Show answer
It is accepted, because with jwk-set-uri Spring does not validate the iss claim. Fix it by building the decoder with NimbusJwtDecoder.withJwkSetUri(...) and calling setJwtValidator(JwtValidators.createDefaultWithIssuer(issuer)), or by switching to issuer-uri.