Challenge - 5 Problems
JWT Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a JWT is missing in the request header?
Consider a Spring Boot JWT validation filter that checks the Authorization header for a JWT token. What is the typical behavior of the filter when the JWT token is missing?
Attempts:
2 left
💡 Hint
Think about security best practices for protected endpoints.
✗ Incorrect
When a JWT token is missing, the filter should reject the request with a 401 status to prevent unauthorized access.
📝 Syntax
intermediate2:00remaining
Which code snippet correctly extracts the JWT token from the Authorization header?
Given the Authorization header value 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', which code correctly extracts the token string after 'Bearer '?
Attempts:
2 left
💡 Hint
Remember that 'Bearer ' includes a space after the word.
✗ Incorrect
The token starts after the 7th character because 'Bearer ' is 7 characters long including the space.
🔧 Debug
advanced2:30remaining
Why does this JWT validation filter always reject valid tokens?
Review this filter code snippet that validates JWT tokens. It always rejects tokens even if they are valid.
```java
String token = authorizationHeader.substring(7);
Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();
if (claims.getExpiration().before(new Date())) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
return;
}
chain.doFilter(request, response);
```
What is the likely cause?
Spring Boot
String token = authorizationHeader.substring(7); Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody(); if (claims.getExpiration().before(new Date())) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return; } chain.doFilter(request, response);
Attempts:
2 left
💡 Hint
Check the logic comparing expiration date with current date.
✗ Incorrect
The condition should reject tokens expired before now, so 'before(new Date())' means token expired. The code is correct logically, but the question states it always rejects valid tokens, so the problem is likely the secretKey is incorrect causing signature validation to fail.
❓ state_output
advanced2:00remaining
What is the value of SecurityContext after a valid JWT is processed?
In a Spring Boot JWT validation filter, after successfully validating a JWT token and extracting user details, the filter sets the authentication in the SecurityContext. What will SecurityContextHolder.getContext().getAuthentication() return?
Attempts:
2 left
💡 Hint
Think about how Spring Security stores user info after authentication.
✗ Incorrect
After validating the JWT, the filter creates an Authentication object (like UsernamePasswordAuthenticationToken) and sets it in the SecurityContext, so it holds user identity and roles.
🧠 Conceptual
expert3:00remaining
Why should a JWT validation filter be stateless and not store session data?
In designing a JWT validation filter for a Spring Boot application, why is it important that the filter remains stateless and does not store session data on the server?
Attempts:
2 left
💡 Hint
Consider how JWT tokens carry user data and how server memory is affected.
✗ Incorrect
JWT tokens are self-contained and include user claims, so the server does not need to keep session state. This improves scalability and simplifies load balancing.