0
0
Spring Bootframework~10 mins

Authentication with JWT token 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 generate a JWT token using the secret key.

Spring Boot
String token = Jwts.builder().setSubject(username).signWith([1]).compact();
Drag options to blanks, or click blank then click option'
AKeys.hmacShaKeyFor(secret.getBytes())
Bnew SecretKeySpec(secret.getBytes(), "HmacSHA256")
CMac.getInstance("HmacSHA256")
DSecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
Attempts:
3 left
💡 Hint
Common Mistakes
Using Mac or SecretKeyFactory directly instead of Keys utility.
Passing raw secret string instead of a Key object.
2fill in blank
medium

Complete the code to extract the username from the JWT token.

Spring Boot
String username = Jwts.parserBuilder().setSigningKey([1]).build().parseClaimsJws(token).getBody().getSubject();
Drag options to blanks, or click blank then click option'
AKeys.hmacShaKeyFor(secret.getBytes())
Bsecret
Ctoken
Dnew SecretKeySpec(secret.getBytes(), "HmacSHA256")
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the raw secret string instead of a Key object.
Passing the token string as the key.
3fill in blank
hard

Fix the error in the code that validates the JWT token expiration.

Spring Boot
boolean isValid = !Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody().getExpiration().[1](new Date());
Drag options to blanks, or click blank then click option'
AcompareTo
Bafter
Cequals
Dbefore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'after' without negation causes wrong validation.
Using 'equals' or 'compareTo' incorrectly.
4fill in blank
hard

Fill both blanks to create a JWT token with expiration time set to 1 hour from now.

Spring Boot
String token = Jwts.builder().setSubject(user).setExpiration(new Date(System.currentTimeMillis() [1] [2])).signWith(key).compact();
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using division or wrong operators.
5fill in blank
hard

Fill all three blanks to parse the JWT token, extract claims, and get the 'roles' claim as a list.

Spring Boot
Claims claims = Jwts.parserBuilder().setSigningKey([1]).build().parseClaimsJws(token).getBody(); List<String> roles = claims.get([2], [3]);
Drag options to blanks, or click blank then click option'
Akey
B"roles"
CList.class
DString.class
Attempts:
3 left
💡 Hint
Common Mistakes
Using String.class instead of List.class for roles.
Passing raw secret string instead of key.