Rotating Signing Keys and Key Management
Learn why and how to rotate JWT signing keys safely, using key IDs (kid), JWK sets, and overlapping validity to avoid downtime.
Rotating Signing Keys and Key Management is a free Spring Security 6 & JWT Authentication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Security 6 & JWT Authentication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Rotate Keys?
A signing key is the secret that proves a JWT is genuine. If it leaks, an attacker can forge tokens. Key rotation replaces keys periodically so a compromised key has a limited lifetime.
The Rotation Challenge
You cannot simply swap the key: tokens signed with the old key are still valid until they expire. The server must accept the old and new keys at the same time during a transition window.
The kid Header
The JWT header can carry a kid (key ID). It tells the verifier which key signed this token, so the server can look up the right key among several.
{
'alg': 'RS256',
'typ': 'JWT',
'kid': 'key-2024-06'
}Signing With a kid
When you mint a token, stamp the current key's id into the header so verifiers can find the matching public key later.
const token = sign(payload, privateKey, {
algorithm: 'RS256',
keyid: 'key-2024-06'
});Keeping a Key Map
The server holds a map of kid to key. During rotation it contains both the retiring key and the new key.
const keys = {
'key-2024-06': newPublicKey,
'key-2024-03': oldPublicKey
};Verifying by kid
On verification, read the kid from the header, select the key, then validate the signature against it.
const header = decodeHeader(token);
const key = keys[header.kid];
const claims = verify(token, key, { algorithms: ['RS256'] });JWK and JWKS
A JWK (JSON Web Key) is a public key in JSON form. A JWKS (JWK Set) is a list of them, typically served at a well-known URL so resource servers can fetch current public keys automatically.
{
'keys': [
{ 'kid': 'key-2024-06', 'kty': 'RSA', 'n': '...', 'e': 'AQAB' }
]
}Spring Security JwtDecoder from JWKS
A Spring resource server can build a decoder straight from a JWKS endpoint, so rotation requires no redeploy of clients.
JwtDecoder decoder = NimbusJwtDecoder
.withJwkSetUri('https://auth.example.com/.well-known/jwks.json')
.build();The Rotation Timeline
A safe rotation follows phases:
- Publish the new key in the JWKS, but keep signing with the old key
- Switch signing to the new key
- Wait for all old tokens to expire
- Remove the old key
Asymmetric vs Symmetric
Rotation is easier with asymmetric keys (RS256/ES256): you can share public keys freely via JWKS while keeping the private key secret. Symmetric (HS256) requires distributing the shared secret to every verifier.
Storing Private Keys Safely
Never commit signing keys to source control. Store them in a secrets manager (Vault, AWS Secrets Manager, KMS) and load them at runtime. Limit who and what can read them.
Quick Check
Test your understanding of key rotation.
Recap
You learned to rotate JWT signing keys safely:
- Rotation limits the damage of a leaked key
- Use
kidheaders so multiple keys can coexist - Publish public keys via a JWKS endpoint
- Rotate in phases and store private keys in a secrets manager
Asymmetric keys plus JWKS make rotation seamless and downtime-free.
Frequently asked questions
Is the “Rotating Signing Keys and Key Management” lesson free?
Yes — the full text of “Rotating Signing Keys and Key Management” is free to read here on the web, and the Spring Security 6 & JWT Authentication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Security 6 & JWT Authentication course, upgrade to CoddyKit PRO.
What will I learn in “Rotating Signing Keys and Key Management”?
Learn why and how to rotate JWT signing keys safely, using key IDs (kid), JWK sets, and overlapping validity to avoid downtime. You practise Spring Security 6 & JWT Authentication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Security 6 & JWT Authentication?
No prior experience is required. Spring Security 6 & JWT Authentication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rotating Signing Keys and Key Management” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Security 6 & JWT Authentication lesson?
Yes. Every Spring Security 6 & JWT Authentication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Implementing Refresh Tokens
- JWT Token Revocation Strategies
- Secure Token Storage Practices
- Rotating Signing Keys and Key Management