Challenge - 5 Problems
JWT Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this JWT token validation method?
Consider this Spring Boot method that validates a JWT token. What will be the returned value if the token is expired?
Spring Boot
public boolean validateToken(String token) {
try {
Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token);
return true;
} catch (ExpiredJwtException e) {
return false;
} catch (JwtException e) {
return false;
}
}Attempts:
2 left
💡 Hint
Think about what happens when the token is expired and how exceptions are handled.
✗ Incorrect
The method catches ExpiredJwtException and returns false when the token is expired. So the output is false.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a JWT token with a subject and expiration?
Select the code snippet that correctly creates a JWT token with subject "user123" and expiration 1 hour from now using jjwt library.
Attempts:
2 left
💡 Hint
Expiration requires a Date object representing a future time.
✗ Incorrect
Option A correctly sets expiration as a Date object 1 hour ahead. Option A passes a long instead of Date, causing error. Option A adds millis to a long, not Date. Option A sets expiration in the past.
🔧 Debug
advanced2:00remaining
Why does this JWT token parsing code throw a SignatureException?
Given this code snippet, why does parsing the JWT token throw a SignatureException?
Spring Boot
Jwts.parserBuilder().setSigningKey(wrongKey).build().parseClaimsJws(token);
Attempts:
2 left
💡 Hint
Think about what the signature in JWT ensures.
✗ Incorrect
SignatureException occurs when the signature does not match. This happens if the key used to parse is different from the signing key.
❓ state_output
advanced2:00remaining
What is the value of 'username' after extracting from this JWT token?
Assuming the JWT token contains a claim 'sub' with value 'alice', what will be the value of 'username' after this code runs?
Spring Boot
Claims claims = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token).getBody(); String username = claims.getSubject();
Attempts:
2 left
💡 Hint
The 'sub' claim is accessed by getSubject() method.
✗ Incorrect
The getSubject() method returns the value of the 'sub' claim, which is 'alice'.
🧠 Conceptual
expert2:00remaining
Which statement best explains why JWT tokens are stateless in authentication?
Why are JWT tokens considered stateless in authentication systems?
Attempts:
2 left
💡 Hint
Think about where the user data lives in JWT authentication.
✗ Incorrect
JWT tokens carry user data and claims inside themselves, so servers do not need to keep session state, making them stateless.