Think about what 'stateless' means for the server's role in managing sessions.
In stateless authentication, the server does not keep session data. The client holds the token and deletes it on logout. The server simply verifies tokens on each request without storing them.
Remember that stateless means no session store on the server.
The server checks the token's expiration by decoding it. If expired, it rejects the request with an unauthorized error. No session lookup is done.
public class JwtAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String token = extractToken(request); if (token != null && validateToken(token)) { UsernamePasswordAuthenticationToken auth = getAuthentication(token); SecurityContextHolder.getContext().setAuthentication(auth); } filterChain.doFilter(request, response); } }
Stateless authentication requires no server session and CSRF protection is usually disabled for APIs.
Setting session management to STATELESS ensures no session is created. Disabling CSRF is common for stateless APIs because tokens protect requests.
public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
return true;
} catch (ExpiredJwtException e) {
return false;
} catch (JwtException e) {
return false;
}
}Check if the key used to verify tokens matches the signing key.
If the secret key used to parse tokens differs from the signing key, all tokens will fail validation and be rejected.
Think about the order of client requests and server validations in token refresh.
The client first sends the access token. The server validates it. If expired, the client requests a new token using the refresh token. The server verifies the refresh token and issues a new access token.