0Pricing
Spring Security 6 & JWT Authentication · บทเรียน

การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ

เรียนรู้วิธีลดภาระการตรวจสอบ JWT เมื่อมีการรับส่งข้อมูลสูง ด้วยการแคชคีย์ JWKS และผลการตรวจสอบโดยไม่ลดทอนความปลอดภัย

การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Cost of Validation

Every request to a JWT-protected API runs signature verification and claim checks. At thousands of requests per second, repeated work, especially fetching public keys, becomes a bottleneck.

What Is Safe to Cache

Not everything should be cached. Safe to cache:

  • The public keys (JWKS) used to verify signatures
  • Expensive parsed metadata

Risky: caching a final allow decision for too long can let a revoked token slip through.

Caching the JWKS

Fetching the JWKS endpoint on every request is wasteful. Cache the key set and refresh it periodically or when an unknown kid appears.

// pseudo: refresh keys at most once per 10 minutes
if (now - keysFetchedAt > 600000) {
  keys = fetchJwks();
  keysFetchedAt = now;
}

Refresh on Unknown kid

If a token carries a kid not in the cache, the signing key may have rotated. Force a one-time refresh before rejecting, so legitimate new tokens are accepted promptly.

let key = keys[kid];
if (!key) { keys = fetchJwks(); key = keys[kid]; }
if (!key) reject('unknown key');

Local Verification Beats Introspection

Self-contained JWTs can be verified locally with the cached public key, avoiding a network call per request. This is far faster than remote token introspection.

Short-Lived Decision Cache

You may cache the parsed claims for a token's lifetime keyed by the token hash, but the cache entry's TTL must never exceed the token's own exp.

ttl = Math.min(claims.exp - now, MAX_CACHE_TTL);
cache.set(hash(token), claims, ttl);

The Revocation Tradeoff

Caching a decision means a revoked token might still be accepted until the cache entry expires. Keep this TTL short (seconds) when you support revocation, so the stale window stays tiny.

Spring's Built-In JWKS Cache

Spring's NimbusJwtDecoder already caches the JWKS internally and handles refresh, so for many apps you get caching for free just by configuring the JWK set URI.

JwtDecoder decoder = NimbusJwtDecoder
    .withJwkSetUri(jwksUri)
    .build();

Measuring the Win

Always measure before and after. Track average validation latency and JWKS fetch count. Caching that does not move your metrics adds complexity for no gain.

Cache Stampede Protection

When a cached key expires, many requests may refresh at once. Use a single-flight lock so only one thread fetches the new JWKS while others wait.

if (refreshing) await refreshPromise;
else { refreshing = true; refreshPromise = fetchJwks(); }

Distributed Caches

In a multi-instance deployment, a shared cache like Redis avoids each node refetching keys independently and keeps a consistent view of revocation state.

Quick Check

Test your understanding of caching token validation.

Recap

You learned to scale JWT validation with caching:

  • Cache the JWKS public keys; refresh on unknown kid
  • Local verification avoids per-request network calls
  • Decision caches must respect the token's exp and stay short when revocation matters
  • Use single-flight refresh and distributed caches at scale

Smart caching cuts latency while keeping security intact.

คำถามที่พบบ่อย

บทเรียน “การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ”

เรียนรู้วิธีลดภาระการตรวจสอบ JWT เมื่อมีการรับส่งข้อมูลสูง ด้วยการแคชคีย์ JWKS และผลการตรวจสอบโดยไม่ลดทอนความปลอดภัย คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. JWT อายุสั้นและวงจรการรีเฟรช
  2. บัญชีดำและบัญชีขาวของ JWT
  3. ข้อควรพิจารณาด้านประสิทธิภาพของ JWT
  4. การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ
← กลับไปที่ Spring Security 6 & JWT Authentication