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

JWTの有効期限と検証ルール

exp、nbf、iatなどの時間ベースのクレームでJWTの有効期間を制御する方法と、検証機能が期限切れまたは有効期限前のトークンを拒否する仕組みを理解します。

「JWTの有効期限と検証ルール」はCoddyKit上の無料Spring Security 6 & JWT Authenticationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Security 6 & JWT Authentication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Tokens Need a Lifetime

A JWT is a bearer credential: whoever holds it is trusted. If a token never expired, a leaked token would grant access forever.

Time-based claims limit the damage window by making tokens valid only for a short period.

The exp Claim

The exp (expiration) claim is a Unix timestamp. After this moment the token is invalid and must be rejected.

{
  'sub': 'user123',
  'exp': 1717200000
}

The iat Claim

The iat (issued at) claim records when the token was created. It is useful for measuring token age and for revoking all tokens issued before a certain time.

{
  'sub': 'user123',
  'iat': 1717196400,
  'exp': 1717200000
}

The nbf Claim

The nbf (not before) claim defines the earliest time a token becomes valid. A token used before its nbf time must be rejected, which is handy for tokens scheduled to activate later.

{
  'sub': 'user123',
  'nbf': 1717196400,
  'exp': 1717200000
}

Choosing an Expiration Window

Short lifetimes are safer but force frequent re-authentication. A common pattern is:

  • Access token: 5-15 minutes
  • Refresh token: days or weeks

The short access token limits exposure; the refresh token keeps the user logged in.

Setting exp When Issuing

When you build a token, set exp relative to now. Here a 15-minute access token is created using a JWT library.

const now = Math.floor(Date.now() / 1000);
const payload = {
  sub: 'user123',
  iat: now,
  exp: now + 15 * 60
};

Validating exp on the Server

On every request the server checks exp against the current time. Most libraries do this automatically and throw if the token is expired.

try {
  const claims = verify(token, secret);
} catch (err) {
  if (err.name === 'TokenExpiredError') {
    // reject with 401
  }
}

Clock Skew

Servers do not always have perfectly synchronized clocks. A small leeway (a few seconds) prevents valid tokens from being rejected because of minor clock differences.

verify(token, secret, { clockTolerance: 5 });

exp Is Not Encryption

Remember: a JWT payload is only encoded, not encrypted. The exp claim stops the server from accepting the token, but anyone can read the claims. Never put secrets in the payload.

Reacting to Expiry on the Client

When the client gets a 401 due to expiry, it should silently request a new access token using the refresh token, then retry the original request.

if (response.status === 401) {
  const fresh = await refreshAccessToken();
  return retryWith(fresh);
}

Common Validation Mistakes

Watch out for these errors:

  • Forgetting to validate exp at all
  • Using milliseconds instead of seconds for the timestamp
  • Setting an excessively long lifetime
  • Ignoring nbf, allowing premature use

Quick Check

Test your knowledge of JWT time claims.

Recap

You learned how JWT lifetime is controlled:

  • exp sets the expiration; iat records issue time; nbf sets earliest validity
  • Use short access tokens plus longer refresh tokens
  • Allow small clock skew with leeway
  • exp does not encrypt the payload

Proper expiration handling keeps stolen tokens useful for only a brief window.

よくある質問

「JWTの有効期限と検証ルール」レッスンは無料ですか?

はい。「JWTの有効期限と検証ルール」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Security 6 & JWT Authenticationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。

「JWTの有効期限と検証ルール」で何を学びますか?

exp、nbf、iatなどの時間ベースのクレームでJWTの有効期間を制御する方法と、検証機能が期限切れまたは有効期限前のトークンを拒否する仕組みを理解します。 ブラウザで直接実行するハンズオンコードでSpring Security 6 & JWT Authenticationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「JWTの有効期限と検証ルール」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. JSON Web Tokenの理解
  2. JWTの構造とクレーム
  3. JWTの署名と検証
  4. JWTの有効期限と検証ルール
← Spring Security 6 & JWT Authenticationに戻る