0Pricing
Spring Security 6 & JWT Authentication · درس

تدوير مفاتيح التوقيع وإدارة المفاتيح

تعلّم سبب تدوير مفاتيح توقيع JWT بأمان وكيفيته، باستخدام معرّفات المفاتيح (kid) ومجموعات JWK وفترات الصلاحية المتداخلة لتجنّب التوقف

تدوير مفاتيح التوقيع وإدارة المفاتيح درس مجاني في Spring Security 6 & JWT Authentication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 kid headers 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.

الأسئلة الشائعة

هل درس «تدوير مفاتيح التوقيع وإدارة المفاتيح» مجاني؟

نعم — نص درس «تدوير مفاتيح التوقيع وإدارة المفاتيح» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Security 6 & JWT Authentication، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Security 6 & JWT Authentication 4 دروس في المجموع.

ماذا ستتعلم في «تدوير مفاتيح التوقيع وإدارة المفاتيح»؟

تعلّم سبب تدوير مفاتيح توقيع JWT بأمان وكيفيته، باستخدام معرّفات المفاتيح (kid) ومجموعات JWK وفترات الصلاحية المتداخلة لتجنّب التوقف تتمرن على Spring Security 6 & JWT Authentication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تنفيذ رموز التحديث
  2. استراتيجيات إبطال رموز JWT
  3. ممارسات التخزين الآمن للرموز
  4. تدوير مفاتيح التوقيع وإدارة المفاتيح
← العودة إلى Spring Security 6 & JWT Authentication