📌 Welcome to Day 38 of our Spring Framework Series!
In the last session, we learned how JWT is structured and how the authentication flow works.
In this session, we’ll see how Spring Security actually checks a JWT token step-by-step using a custom JWT filter.
✅ What You’ll Learn in this Session:
Request Comes In
• Client sends a request with JWT token in the Authorization header.
• Example: Authorization: Bearer jwt_token
Control Reaches the JWT Filter
• Every request first passes through filters in Spring Security.
• Our custom JWT filter intercepts requests before hitting the controller.
• If no token → request continues as unauthenticated.
• If token is present → move to validation.
Extract & Validate Token
• Remove Bearer prefix.
• Use JwtHelper (or utility class) to:
Verify token signature.
Check expiration.
Parse claims (username, roles, etc.).
Fetch User Details from Payload
• From claims, extract the username (and roles if included).
Load User from Database (Optional)
• Sometimes we reload the user from DB using UserDetailsService to ensure account is still active or roles are updated.
• If roles are already in token → DB lookup can be skipped.
Create Authentication Object
• Build a UsernamePasswordAuthenticationToken:
Principal → username / userDetails
Credentials → null (password not needed here)
Authorities → roles from DB or token
• Example:
UsernamePasswordAuthenticationToken auth =
new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
Set Authentication in Security Context
• Put this object into SecurityContextHolder:
SecurityContextHolder.getContext().setAuthentication(auth);
Proceed with Request
• Now Spring Security treats the request as authenticated.
• The controller executes with the user context available.
✅ Why This is Important:
• Understand how JWT validation actually happens inside Spring Security.
• Learn the exact flow from raw request → filter → authentication → controller.
• Helps in debugging issues like “Invalid token” or “User not authenticated”.
🎯 Perfect For:
👨🎓 Students learning JWT authentication.
🧑💻 Developers building secure REST APIs.
💼 Professionals preparing for interviews.
📚 Key Topics Covered:
• JWT Filter role in Spring Security
• Token extraction & validation
• Fetching user details from claims
• Creating Authentication object
• Setting Authentication in SecurityContext
🔔 Subscribe & press the bell icon to stay updated with the series!
📚 Want Live Java Full Stack Training with Projects?
📞 Call/WhatsApp: +91 8088467640
📺 PLAYLISTS:
CURRENT PLAYLIST: ADVANCED JAVA
• Advanced Java Complete | Alpha Batch Live🔊
COMPLETE SPRING FRAMEWORK
• Playlist
SPRINGBOOT ONLY
• Playlist
RESTAPI ONLY
• Playlist
SPRING SECURITY ONLY
• Playlist
COMPLETE CORE JAVA
• Core Java (Zero to Hero) | Alpha Batch Live🔊
COMPLETE JAVA BACKEND PLAYLIST
• Playlist
#SpringFramework #java #springboot #SpringTutorial #JavaDeveloper #BackendDevelopment #CodeHunt #RESTAPI #springsecurity #JWT