Sub topic 8
Validating issuer, audience, and expiry
This chapter assumes you have a working JWT resource server and know what a JwtDecoder does. You should also know the registered claims iss, aud, exp, and nbf. A valid signature only proves that the issuer's key signed the token. It does not prove that the token is current, or that it was meant for your API.
What the decoder checks by default
When you set only issuer-uri, Spring Boot builds a decoder that verifies the signature, checks the timestamps, and checks iss against the issuer URI. It does not check aud.
| Check | Claim | Default behavior |
|---|---|---|
| Expiry | `exp` | Token rejected after this time |
| Not before | `nbf` | Token rejected before this time |
| Issuer | `iss` | Must equal the configured issuer |
| Audience | `aud` | Not checked |
The timestamp check allows 60 seconds of clock skew, so a token that expired 30 seconds ago may still pass.
Why audience matters
Suppose your authorization server issues tokens for several APIs. A token meant for a billing API is still signed by the same issuer. Without an audience check, your orders API would accept it. The aud claim names the intended recipients, so each API should confirm it appears there.
Adding an audience validator
Build the decoder yourself and combine validators with DelegatingOAuth2TokenValidator. JwtValidators.createDefaultWithIssuer supplies the timestamp and issuer checks. A JwtClaimValidator adds the audience rule.
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.jwt.JwtClaimValidator;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
@Configuration
public class JwtValidationConfig {
@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;
}
}setJwtValidator replaces the default validator. If you pass only the audience validator, you silently drop the expiry and issuer checks. Always include createDefaultWithIssuer or createDefault().
Spring Security exposes the JwtDecoder bean to the resource server configuration. Once you define it, the issuer-uri property is used only by your code, as shown above.
Common errors
- Dropping the defaults. Setting only a custom validator removes the timestamp and issuer checks.
- Wrong audience value. Some providers put the API identifier in
aud, others use ascporazpclaim. Decode a real token and look before writing the rule. - Single-string
aud. The JWT specification allowsaudto be a string or an array. Spring's claim accessor converts it to a list, which is why the example usesList<String>. - Clock drift. If valid tokens are rejected as not yet valid, fix server clocks first. Widen the skew only as a last resort.
A failed validation produces a 401 response with a WWW-Authenticate header containing invalid_token. The details are covered in the chapter on 401 and 403 handling.
Exercise
You define a JwtDecoder that calls setJwtValidator with only an audience validator. Tokens from the correct issuer with the right audience are accepted. What else is now accepted that should not be, and how do you fix it?
Show answer
Expired tokens and tokens from other issuers are now accepted, because the default timestamp and issuer validators were replaced. Fix it by combining the audience validator with JwtValidators.createDefaultWithIssuer(issuer) in a DelegatingOAuth2TokenValidator.