กลยุทธ์การเพิกถอนโทเค็น JWT
เรียนรู้วิธีเพิกถอน JWT ที่ถูกขโมยหรือผู้ใช้ออกจากระบบแล้ว เช่น การทำบัญชีดำและการใช้โทเค็นอายุสั้น
กลยุทธ์การเพิกถอนโทเค็น JWT เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Revoke JWTs?
JSON Web Tokens (JWTs) are designed to be stateless, meaning the server doesn't need to store session information. While this offers great scalability, it presents a challenge: how do you invalidate a token before its natural expiration?
We need revocation for scenarios like:
- User logout
- Token compromise (e.g., stolen token)
- User role change or account disablement
The Statelessness Challenge
A core principle of JWTs is that once issued and signed, they can be validated without needing to query a database or external service. This means a server doesn't inherently 'know' if a token has been logically invalidated.
To revoke a JWT, you must introduce a mechanism that re-introduces a form of state, allowing the server to check if a token is still considered valid.
Strategy 1: Short-Lived Tokens
The simplest and most fundamental defense against compromised JWTs is to make them short-lived. If an access token expires quickly (e.g., 5-15 minutes), the window of opportunity for an attacker using a stolen token is minimized.
This strategy often pairs with Refresh Tokens (covered in another lesson) to provide a smooth user experience without requiring frequent re-logins.
Strategy 2: Blacklisting Tokens
To achieve immediate revocation, a common strategy is blacklisting. A blacklist is a storage (like a database table or a high-speed cache like Redis) that holds the unique identifiers (JTI claims) of tokens that have been explicitly invalidated.
When a server receives a JWT, it first checks if the token's JTI is present in the blacklist. If it is, the token is rejected, even if it hasn't expired.
Implementing a Blacklist
For an effective blacklist:
- Unique ID: Ensure each JWT has a unique JTI (JWT ID) claim.
- Storage: Use a fast, persistent store (e.g., Redis, database table) to hold blacklisted JTIs.
- Check: Every time a JWT is presented, validate its signature, then check if its JTI is in the blacklist.
- Expiration: Blacklisted tokens should still have their expiry respected. The blacklist entry itself can also have a TTL (Time To Live) matching the token's original expiry, to prevent the list from growing indefinitely.
Simulating a Blacklist
Let's look at a simple Java example to understand the blacklisting concept. We'll simulate a token's unique ID and an in-memory blacklist. In a real application, the blacklist would be a persistent, distributed store like Redis.
Blacklist in Action
This code demonstrates how a token ID can be added to a conceptual blacklist and then checked for revocation. Run it to see the output.
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class Main {
private static Set<String> revokedTokens = new HashSet<>();
public static void main(String[] args) {
String userTokenId = UUID.randomUUID().toString();
System.out.println("User token ID: " + userTokenId);
// Simulate token validation
if (!isTokenRevoked(userTokenId)) {
System.out.println("Token is valid (not revoked).");
} else {
System.out.println("Token is revoked!");
}
// User logs out or token is compromised
revokeToken(userTokenId);
System.out.println("\n--- Token has been revoked ---");
// Try to validate again
if (!isTokenRevoked(userTokenId)) {
System.out.println("Token is valid (not revoked).");
} else {
System.out.println("Token is revoked!");
}
}
public static void revokeToken(String tokenId) {
revokedTokens.add(tokenId);
}
public static boolean isTokenRevoked(String tokenId) {
return revokedTokens.contains(tokenId);
}
}Strategy 3: Whitelisting
An alternative, though less common for raw JWTs, is whitelisting. Instead of listing invalid tokens, you maintain a list of all currently valid tokens or session IDs.
When a request comes in, you check if the token/session ID is on this 'allow list'. If it's not present, it's considered invalid. This approach is more typical for traditional session management but can be adapted.
- Every active session gets a unique ID.
- Store these valid IDs in a database or cache.
- Upon logout, remove the ID from the whitelist.
Comparing Strategies
Each strategy has its place:
- Short-Lived Tokens: Essential for minimizing risk. Always combine with other strategies.
- Blacklisting: Best for specific, immediate invalidation (e.g., logout, compromise). Introduces a state check per request.
- Whitelisting: Useful when you need to manage a finite set of active sessions and invalidate many at once (e.g., user disabled, revoke all sessions). Requires state for *all* tokens, which can be a performance consideration for very high-traffic APIs.
Revocation Strategies Check
Which of the following are valid strategies or important considerations for revoking JSON Web Tokens (JWTs) before their natural expiration?
Lesson Summary
In this lesson, we explored how to tackle the challenge of revoking stateless JWTs. We learned that while JWTs are stateless by design, scenarios like user logout or token compromise necessitate invalidation.
Key strategies include:
- Using short-lived tokens to minimize risk.
- Implementing a blacklist to mark specific tokens as invalid using their JTI.
- Considering whitelisting for managing active sessions, though less common for raw JWTs.
The most robust solutions often combine short expiry times with a blacklisting mechanism for immediate revocation needs.
คำถามที่พบบ่อย
บทเรียน “กลยุทธ์การเพิกถอนโทเค็น JWT” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “กลยุทธ์การเพิกถอนโทเค็น JWT” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การเพิกถอนโทเค็น JWT”
เรียนรู้วิธีเพิกถอน JWT ที่ถูกขโมยหรือผู้ใช้ออกจากระบบแล้ว เช่น การทำบัญชีดำและการใช้โทเค็นอายุสั้น คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “กลยุทธ์การเพิกถอนโทเค็น JWT” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม
ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การนำโทเค็นรีเฟรชไปใช้งาน
- กลยุทธ์การเพิกถอนโทเค็น JWT
- แนวทางการจัดเก็บโทเค็นอย่างปลอดภัย
- การหมุนเวียนคีย์ลายเซ็นและการจัดการคีย์