0Pricing
Spring Security 6 & JWT Authentication · レッスン

署名キーのローテーションとキー管理

キーID(kid)、JWKセット、重複する有効期間を使い、ダウンタイムを避けながらJWT署名キーを安全にローテーションする理由と方法を学びます。

「署名キーのローテーションとキー管理」はCoddyKit上の無料Spring Security 6 & JWT Authenticationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、Spring Security 6 & JWT Authenticationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。

「署名キーのローテーションとキー管理」で何を学びますか?

キーID(kid)、JWKセット、重複する有効期間を使い、ダウンタイムを避けながらJWT署名キーを安全にローテーションする理由と方法を学びます。 ブラウザで直接実行するハンズオンコードでSpring Security 6 & JWT Authenticationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Security 6 & JWT Authenticationを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Security 6 & JWT Authenticationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「署名キーのローテーションとキー管理」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Security 6 & JWT Authenticationレッスンでコードを書いて実行できますか?

はい。すべてのSpring Security 6 & JWT Authenticationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. リフレッシュトークンの実装
  2. JWTトークン失効戦略
  3. 安全なトークン保存の実践
  4. 署名キーのローテーションとキー管理
← Spring Security 6 & JWT Authenticationに戻る