Sub topic 15
Key rotation and JWK set caching
This chapter assumes you have a working JWT resource server and know what kid, issuer-uri, and jwk-set-uri mean. Here you will see what happens when the authorization server replaces its signing key, and how the resource server's key cache handles it.
Why keys rotate
The authorization server signs every JWT with a private key. Rotating that key on a schedule limits the damage if it leaks, and it lets you retire old algorithms. Rotation must not break tokens that are already in circulation, and your API should not need a restart.
The mechanism is the JWK set. Each key in the set has a kid (key ID), and each token header names the kid that signed it. The resource server picks the matching public key by that ID.
A safe rotation sequence
- The authorization server adds a new public key to the JWK set. The old key stays.
- It starts signing new tokens with the new key.
- Tokens signed with the old key remain valid until they expire, so the old key stays published until then.
- Once the longest token lifetime has passed, it removes the old key.
If the old key is removed too early, valid tokens fail with a 401.
How Spring Security caches keys
With issuer-uri or jwk-set-uri, NimbusJwtDecoder downloads the JWK set on first use and keeps it in a cache. It does not call the authorization server for every request. When a token names a kid that the cached set does not contain, the decoder fetches the set again and retries. That retry is why a rotation usually needs no action on your side.
Two effects follow from this:
- A token with a random
kidcan trigger a remote fetch. Keep the fetch cheap, and do not expose the JWK endpoint to extra load. - A removed key can stay in a long-lived cache, so tokens signed with it keep validating. Set an expiry on the cache.
Setting an expiry on the cache
You can pass your own Spring Cache to the decoder builder. This example uses Caffeine, so add com.github.ben-manes.caffeine:caffeine and spring-boot-starter-cache to your build.
import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
@Bean
JwtDecoder jwtDecoder(
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}") String jwkSetUri) {
Cache cache = new CaffeineCache("jwks",
Caffeine.newBuilder().expireAfterWrite(Duration.ofMinutes(10)).build());
return NimbusJwtDecoder.withJwkSetUri(jwkSetUri).cache(cache).build();
}After 10 minutes the cached set is dropped and the next token triggers a fresh download. A key removed by the authorization server therefore stops working within that time.
If you define your own JwtDecoder this way, re-add any issuer and audience validators from the earlier chapter. A custom bean replaces the auto-configured one.
Choosing a cache lifetime
| Lifetime | Benefit | Cost |
|---|---|---|
| Short (1 to 5 minutes) | Removed keys stop working quickly | More requests to the JWK endpoint |
| Long (hours) | Few remote calls | Revoked or retired keys stay trusted longer |
A common choice is a few minutes to an hour, shorter than your key retirement window.
Exercise
The authorization server publishes a new key and signs new tokens with it. Your API has the old JWK set cached. What happens to the first token signed with the new key, and why?
Show answer
The decoder finds no cached key for the token's kid. It fetches the JWK set again, finds the new key, and validates the token, so the request succeeds with no restart. This works only while the JWK endpoint is reachable.