0
0
Spring Bootframework~10 mins

Stateless authentication mental model in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the token from the HTTP header.

Spring Boot
String token = request.getHeader([1]);
Drag options to blanks, or click blank then click option'
A"Authorization"
B"Cookie"
C"Content-Type"
D"User-Agent"
Attempts:
3 left
💡 Hint
Common Mistakes
Using Cookie header instead of Authorization.
Trying to get token from Content-Type or User-Agent headers.
2fill in blank
medium

Complete the code to validate the JWT token signature.

Spring Boot
boolean isValid = jwtUtil.validateToken([1]);
Drag options to blanks, or click blank then click option'
Atoken
Brequest
Cauthentication
DuserDetails
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole request object instead of the token string.
Passing user details or authentication objects.
3fill in blank
hard

Fix the error in the code to set the authentication in the security context.

Spring Boot
SecurityContextHolder.getContext().setAuthentication([1]);
Drag options to blanks, or click blank then click option'
AuserDetails
Btoken
CauthenticationToken
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the raw token string instead of an Authentication object.
Passing the request or user details directly.
4fill in blank
hard

Fill both blanks to create a JWT token with a subject and expiration.

Spring Boot
String jwt = Jwts.builder().setSubject([1]).setExpiration([2]).compact();
Drag options to blanks, or click blank then click option'
Ausername
Bnew Date(System.currentTimeMillis() + 3600000)
Cnew Date()
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using password as subject.
Setting expiration to current date (already expired).
5fill in blank
hard

Fill both blanks to extract username, check expiration, and get claims from JWT.

Spring Boot
Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws([1]).getBody(); String username = claims.getSubject(); boolean expired = claims.getExpiration().[2](new Date());
Drag options to blanks, or click blank then click option'
Atoken
Bafter
Cbefore
Dequals
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'after' instead of 'before' to check expiration.
Parsing something other than the token string.