บัญชีดำและบัญชีขาวของ 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 powerful for authentication, but sometimes you need to invalidate them before their natural expiry. This process is called token revocation.
- Compromised Token: If a token is stolen.
- User Logout: To immediately end a user's session.
- Password Change: To invalidate all old tokens.
- Role Changes: To force re-authentication with new permissions.
The Stateless Challenge
JWTs are inherently stateless. Once issued, they contain all necessary information for validation and don't require the server to store session data.
This statelessness is a strength, but it makes direct server-side revocation tricky. The server typically doesn't hold a list of active tokens to simply 'turn off'.
Blacklisting Explained
Blacklisting is a common strategy to revoke JWTs. When a token needs to be invalidated, its unique identifier (often the JTI claim) is added to a 'blacklist' — a list of forbidden tokens.
- Any token whose JTI is on this list is rejected, even if it's cryptographically valid and not expired.
- This allows you to 'undo' a token's validity.
Implementing a Blacklist
The blacklist needs to be stored in a highly available, fast-access data store. Speed is crucial because every incoming request might need to check this list.
- Redis: An excellent choice due to its in-memory nature and support for time-to-live (TTL) on entries, which can match token expiry.
- Database: A simple table can work, but might be slower for high-volume checks.
- Each entry typically stores the JWT's JTI and its original expiry time.
Simple Blacklist Service
Here's a basic interface for a service that manages a token blacklist. In a real application, this would interact with a database or a caching system like Redis.
public interface TokenBlacklistService {
void blacklistToken(String jti, long expirySeconds);
boolean isBlacklisted(String jti);
}JWT Filter with Blacklist Check
When a request arrives, a security filter would first validate the JWT's signature and expiry. Then, it would check if the token's JTI is present on the blacklist before granting access.
Try running this example:
import java.util.HashSet;
import java.util.Set;
// A simplified in-memory blacklist for demonstration
class MockTokenBlacklistService {
private Set<String> blacklistedJtis = new HashSet<>();
public void blacklistToken(String jti, long expirySeconds) {
System.out.println("Action: Blacklisting JTI " + jti);
blacklistedJtis.add(jti);
// In a real app, 'expirySeconds' would set a TTL on the blacklist entry
}
public boolean isBlacklisted(String jti) {
boolean result = blacklistedJtis.contains(jti);
System.out.println("Check: Is JTI " + jti + " blacklisted? " + result);
return result;
}
}
public class Main {
public static void main(String[] args) {
MockTokenBlacklistService blacklist = new MockTokenBlacklistService();
String userTokenJti = "user-abc-123";
String adminTokenJti = "admin-def-456";
// Simulate an admin token being revoked after a security event
blacklist.blacklistToken(adminTokenJti, 3600); // Token expires in 1 hour
// Check access for different tokens
System.out.println("\n--- Access Checks ---");
System.out.println("User token access: " + (blacklist.isBlacklisted(userTokenJti) ? "DENIED" : "GRANTED"));
System.out.println("Admin token access: " + (blacklist.isBlacklisted(adminTokenJti) ? "DENIED" : "GRANTED"));
}
}Whitelisting Explained
Whitelisting is an alternative revocation strategy. Instead of listing forbidden tokens, you maintain a list of active, allowed tokens.
- When a token is issued, its JTI is added to a 'whitelist'.
- For every request, the token's JTI must be found on this whitelist to be considered valid.
- If a token's JTI is not on the whitelist, it's rejected.
Implementing a Whitelist
Similar to blacklisting, a whitelist requires a fast, persistent store (e.g., Redis). The key difference is what you store and how you manage it:
- Each entry typically stores the JWT's JTI, often associated with a user ID.
- When a user logs out or changes their password, all active JTIs associated with that user can be efficiently removed from the whitelist.
Blacklist vs. Whitelist Comparison
Both strategies achieve revocation but have different implications:
- Blacklist: Ideal for rare, specific revocations (e.g., single token compromise). Requires less storage if revocations are infrequent.
- Whitelist: Better for frequent revocations (e.g., user logout invalidates all tokens). Can simplify session management but requires more storage for all active tokens.
- The choice depends on your application's specific needs and the frequency of revocations.
Revocation Scenario
Consider an application where users frequently log out, and you need to ensure all their issued tokens are immediately invalidated upon logout.
Recap: Revocation Strategies
In this lesson, we've explored advanced strategies for revoking JWTs, which is crucial for robust security:
- Blacklisting: Marking specific tokens as invalid by adding their JTI to a forbidden list.
- Whitelisting: Only allowing tokens that are explicitly listed as active, often tied to a user session.
- The best approach depends on your application's requirements, especially the frequency and nature of token invalidation.
Next, we'll analyze the performance implications of these techniques.
คำถามที่พบบ่อย
บทเรียน “บัญชีดำและบัญชีขาวของ JWT” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “บัญชีดำและบัญชีขาวของ JWT” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “บัญชีดำและบัญชีขาวของ 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 อายุสั้นและวงจรการรีเฟรช
- บัญชีดำและบัญชีขาวของ JWT
- ข้อควรพิจารณาด้านประสิทธิภาพของ JWT
- การแคชการตรวจสอบโทเค็นเพื่อรองรับการขยายระบบ