Quick revision
The short answer
We rotate in four phases: generate the new key and publish it in the JWKS next to the old one without using it, wait at least as long as resource servers cache the JWKS, switch signing to the new key by its kid while tokens signed with the old key stay valid, and remove the old key only after the longest-lived token signed with it has expired. The keys live in a secure store or KMS and the steps are automated.
What to remember
In this answer, we rotated the signing key in four phases: publish the new key next to the old one, wait for verifier caches, switch signing by kid, and remove the old key after the longest-lived signed token has expired. In Spring Authorization Server, the JWKSource publishes both keys and an OAuth2TokenCustomizer sets the active kid, because the encoder will not choose between two keys itself. Keys stay in a KMS or secret store, every instance reads the same key ring, and the steps run as an automated, regularly practised job.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We rotate in four phases: generate the new key and publish it in the JWKS next to the old one without using it, wait at least as long as resource servers cache the JWKS, switch signing to the new key by its kid while tokens signed with the old key stay valid, and remove the old key only after the longest-lived token signed with it has expired. The keys live in a secure store or KMS and the steps are automated.
Why this question is asked
Signing keys must change regularly, and sometimes urgently after a suspected leak. With 200 clients and many resource servers validating tokens, the order of steps decides whether anyone notices. Done in the wrong order, every resource server that has not seen the new key rejects every new token at once. Interviewers ask this to check that we understand caching on the verifier side and the life of tokens already issued. In this answer, we will recap how verifiers find keys, walk through the four phases with timings, configure Spring Authorization Server to publish two keys while signing with one, and automate it.
1A Quick Recap of How Verifiers Find Keys
Spring Authorization Server signs each JWT with a private key and writes that key's id, the kid, into the token header. Resource servers download the public keys from the JWKS endpoint, cache them, and pick the key whose kid matches the token. Most resource servers refetch when they see an unknown kid, but with rate limits, and some clients cache the JWKS for a fixed time without refetching. So we cannot assume a verifier sees a new key the moment we add it.
There are therefore two kinds of waiting in any rotation: waiting for verifiers to learn the new key before we use it, and waiting for tokens signed with the old key to expire before we remove it.
2The Four Phases
Assume resource servers cache the JWKS for up to 15 minutes, access tokens live 10 minutes, and ID tokens 1 hour. The timeline below uses those numbers; in practice we add a generous safety margin.
| Phase | JWKS publishes | Signing key | Wait before next phase |
|---|---|---|---|
| 1. Publish | old + new | old | longest JWKS cache, e.g. 24 h with margin |
| 2. Switch | old + new | new | longest token lifetime, e.g. 1 h + margin |
| 3. Retire | new | new | none |
| Steady state | new | new | until the next rotation |
In phase 1 we generate the new key pair and add its public key to the JWKS, but keep signing with the old one. Nothing changes for anyone yet. We wait at least the longest cache time we know of; many teams simply wait a day, because a client with an unusual cache setting is easy to miss among 200.
In phase 2 we switch signing to the new key. New tokens carry the new kid, which every verifier has already cached. Tokens signed earlier with the old key still verify, because the old key is still published.
In phase 3, once the longest token signed with the old key has expired, here one hour for ID tokens, we remove the old public key from the JWKS. Opaque refresh tokens are not signed JWTs, so they do not hold us back; if refresh tokens are JWTs too, their lifetime counts as well.
3Publishing Two Keys but Signing With One
In Spring Authorization Server, the JWKSource serves two purposes: the JWKS endpoint publishes the public part of every key it returns, and the JWT encoder picks a signing key from it. But there is a catch: if the encoder finds several keys suitable for the algorithm and the token header names no kid, it refuses to choose and token issuing fails with an error asking for a key id (check your version for the exact message). So during phases 1 and 2 we must tell it which key is active.
Notice that the KeyRing is read on every call rather than captured once at startup, so all instances of the server pick up a phase change without a restart. Every instance must also see the same key ring, or one instance would sign with a key another instance does not publish.
4Where the Keys Live
The private keys are the crown jewels: anyone with one can mint tokens for all 200 clients. So they never sit in the application's configuration or image. We keep them in a key management service or HSM, or at least in a secret store such as Vault with tight access rules, and the KeyRing loads them from there. Some KMS products can sign without ever releasing the private key, which is the strongest option but means a custom signing step instead of the default encoder.
5Automating the Rotation
Manual rotation is risky because it happens rarely and the waits are long. We write it as a scheduled job that moves through the phases, checks before each step that the conditions hold, for example fetching the JWKS from outside and confirming both kids are present, and records the phase in the shared store. We run it every 90 days so the procedure stays practised, and we keep a documented emergency path: if a key leaks, we skip the first wait and accept that some verifiers will briefly reject new tokens, because accepting forged tokens is worse.
6Follow-up questions
What if a resource server never refetches the JWKS?
Then it will reject tokens after phase 2 until it restarts. That is why we wait well beyond the normal cache time in phase 1, announce rotations to client owners, and test in staging that our standard resource server configuration picks up a new key on its own.
Why not remove the old key at the same moment we switch?
Because tokens signed a minute earlier are still valid for their whole lifetime. Their verifiers would find no key for the old kid and reject them, which logs out users and breaks service calls for up to the token lifetime.
Does rotating the signing key invalidate refresh tokens?
Not if the refresh tokens are opaque, which is Spring Authorization Server's default, because they are looked up in the authorization service rather than verified by signature. New access tokens issued from them are simply signed with the new key.
7Conclusion
In this answer, we rotated the signing key in four phases: publish the new key next to the old one, wait for verifier caches, switch signing by kid, and remove the old key after the longest-lived signed token has expired. In Spring Authorization Server, the JWKSource publishes both keys and an OAuth2TokenCustomizer sets the active kid, because the encoder will not choose between two keys itself. Keys stay in a KMS or secret store, every instance reads the same key ring, and the steps run as an automated, regularly practised job.
Open with
I would do it in phases so that no valid token ever points at a key that is not published. First, I generate the new key in our KMS or secret store and add its public key to the JWKS alongside the old one, but keep signing with the old key.
Then, if they want more
Then I wait at least as long as resource servers cache the JWKS; with 200 clients I would wait something like a day rather than the theoretical fifteen minutes. Second, I switch signing to the new key, selected by its kid. Tokens signed earlier keep working because the old key is still published.
Third, once the longest-lived token signed with the old key has expired, say the one-hour ID token lifetime plus margin, I remove the old key from the JWKS.
In Spring Authorization Server, the JWKSource returns both keys during the overlap, and I add an OAuth2TokenCustomizer that sets the active kid in the JWS header, because the encoder refuses to pick between two suitable keys on its own. All instances read the same key ring, so they switch together.
Finally, I automate the phases as a scheduled job with checks between steps, run it every few months so it stays practised, and keep an emergency path for a leaked key.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RFC 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You run Spring Authorization Server for 200 clients. You must rotate the token signing...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/d370d98f36/