การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์
เรียนรู้เหตุผลและวิธีหมุนเวียนคีย์ลายเซ็นของ JWT อย่างปลอดภัย โดยใช้รหัสคีย์ (kid) ชุด JWK และช่วงเวลาที่ใช้งานทับซ้อนกันเพื่อหลีกเลี่ยงการหยุดให้บริการ
การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์ เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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.
คำถามที่พบบ่อย
บทเรียน “การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์”
เรียนรู้เหตุผลและวิธีหมุนเวียนคีย์ลายเซ็นของ JWT อย่างปลอดภัย โดยใช้รหัสคีย์ (kid) ชุด JWK และช่วงเวลาที่ใช้งานทับซ้อนกันเพื่อหลีกเลี่ยงการหยุดให้บริการ คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม
ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำโทเค็นรีเฟรชไปใช้งาน
- กลยุทธ์การเพิกถอนโทเค็น JWT
- แนวทางการจัดเก็บโทเค็นอย่างปลอดภัย
- การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์