Rotacja kluczy podpisujących i zarządzanie kluczami
Dowiedz się, dlaczego i jak bezpiecznie rotować klucze podpisujące JWT, korzystając z identyfikatorów kluczy (kid), zestawów JWK i nakładających się okresów ważności, aby uniknąć przestojów.
Rotacja kluczy podpisujących i zarządzanie kluczami to bezpłatna lekcja Spring Security 6 & JWT Authentication na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Security 6 & JWT Authentication, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Security 6 & JWT Authentication zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Ucz się Java dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 12
- Lekcje
- 48
Często zadawane pytania
Czy lekcja „Rotacja kluczy podpisujących i zarządzanie kluczami” jest bezpłatna?
Tak — pełny tekst „Rotacja kluczy podpisujących i zarządzanie kluczami” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Security 6 & JWT Authentication, przejdź na CoddyKit PRO. Kurs Spring Security 6 & JWT Authentication zawiera 4 lekcji w sumie.
Co nauczysz się w „Rotacja kluczy podpisujących i zarządzanie kluczami”?
Dowiedz się, dlaczego i jak bezpiecznie rotować klucze podpisujące JWT, korzystając z identyfikatorów kluczy (kid), zestawów JWK i nakładających się okresów ważności, aby uniknąć przestojów. Ćwiczysz Spring Security 6 & JWT Authentication z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Spring Security 6 & JWT Authentication?
Nie wymagamy żadnego doświadczenia. Spring Security 6 & JWT Authentication w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Rotacja kluczy podpisujących i zarządzanie kluczami”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Spring Security 6 & JWT Authentication?
Tak. Każda lekcja Spring Security 6 & JWT Authentication zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Implementacja tokenów odświeżania
- Strategie unieważniania tokenów JWT
- Bezpieczne przechowywanie tokenów
- Rotacja kluczy podpisujących i zarządzanie kluczami