0
0
Spring Bootframework~20 mins

Authentication with JWT token in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JWT Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
    }
}
Afalse
BThrows ExpiredJwtException
Ctrue
Dnull
Attempts:
2 left
💡 Hint
Think about what happens when the token is expired and how exceptions are handled.
📝 Syntax
intermediate
2: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.
AString token = Jwts.builder().setSubject("user123").setExpiration(new Date(System.currentTimeMillis() + 3600000)).signWith(key).compact();
BString token = Jwts.builder().setSubject("user123").setExpiration(System.currentTimeMillis() + 3600000).signWith(key).compact();
CString token = Jwts.builder().setSubject("user123").setExpiration(new Date().getTime() + 3600000).signWith(key).compact();
DString token = Jwts.builder().setSubject("user123").setExpiration(new Date(System.currentTimeMillis() - 3600000)).signWith(key).compact();
Attempts:
2 left
💡 Hint
Expiration requires a Date object representing a future time.
🔧 Debug
advanced
2: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);
AThe token is null or empty.
BThe token is expired and cannot be parsed.
CThe token format is invalid and missing parts.
DThe signing key used to parse the token does not match the key used to sign it.
Attempts:
2 left
💡 Hint
Think about what the signature in JWT ensures.
state_output
advanced
2: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();
Anull
B"alice"
CThrows NullPointerException
D"sub"
Attempts:
2 left
💡 Hint
The 'sub' claim is accessed by getSubject() method.
🧠 Conceptual
expert
2:00remaining
Which statement best explains why JWT tokens are stateless in authentication?
Why are JWT tokens considered stateless in authentication systems?
ABecause the server stores all tokens in a database to track user sessions.
BBecause JWT tokens expire immediately after creation, forcing re-authentication.
CBecause all user information and claims are stored inside the token itself, so no server-side session storage is needed.
DBecause JWT tokens require a server to validate each token against a session store.
Attempts:
2 left
💡 Hint
Think about where the user data lives in JWT authentication.