Think about how JWT helps avoid server-side session storage.
JWT allows the server to verify user identity by checking the token signature without keeping session data, making APIs stateless and scalable.
Think about how JWT expiration affects authentication.
Expired JWT tokens are invalid, so the API rejects the request with a 401 error to prevent unauthorized access.
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
Claims claims = Jwts.parserBuilder()
.setSigningKey(secretKey)
.build()
.parseClaimsJws(token)
.getBody();
// Extract username hereJWT standard uses a specific claim for the principal subject.
The 'sub' claim holds the principal identity, accessible via getSubject() method in Claims.
Think about how Spring Security handles missing authentication tokens.
Without a JWT token, the API cannot authenticate the user, so it returns 401 Unauthorized.
Consider what happens if parseClaimsJws throws an exception and it is not handled.
If exceptions from parseClaimsJws are not caught, the application may continue processing without noticing the token is invalid.