0
0
Spring Bootframework~10 mins

JWT structure (header, payload, signature) 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 create the JWT header with the correct algorithm.

Spring Boot
Map<String, Object> header = new HashMap<>();
header.put("alg", "[1]");
Drag options to blanks, or click blank then click option'
ASHA256
BRSA
CHS256
DMD5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'SHA256' directly instead of 'HS256'.
Using 'MD5' which is insecure and not standard for JWT.
2fill in blank
medium

Complete the code to add the subject claim to the JWT payload.

Spring Boot
Map<String, Object> payload = new HashMap<>();
payload.put("sub", "[1]");
Drag options to blanks, or click blank then click option'
Ausername
Bsubject
CuserId
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the literal word 'subject' instead of the username.
Using 'token' which is unrelated to the 'sub' claim.
3fill in blank
hard

Fix the error in signing the JWT token with the secret key.

Spring Boot
String token = Jwts.builder()
  .setHeader(header)
  .setClaims(payload)
  .signWith(Keys.hmacShaKeyFor("[1]".getBytes()))
  .compact();
Drag options to blanks, or click blank then click option'
Amysecret
Bkey
Csecret
Dmysecretkeymysecretkeymysecretkeymysecretkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using too short secret keys causing runtime errors.
Using generic words like 'key' or 'secret' without enough length.
4fill in blank
hard

Fill both blanks to decode the JWT token and extract the payload claims.

Spring Boot
Claims claims = Jwts.parserBuilder()
  .setSigningKey(Keys.hmacShaKeyFor("[1]".getBytes()))
  .build()
  .[2](token)
  .getBody();
Drag options to blanks, or click blank then click option'
Amysecretkeymysecretkeymysecretkeymysecretkey
BparseClaimsJws
CparsePlaintextJwt
Dmysecret
Attempts:
3 left
💡 Hint
Common Mistakes
Using a short secret key that doesn't match the signing key.
Using parsePlaintextJwt which is for unsigned tokens.
5fill in blank
hard

Fill all three blanks to build a JWT token with header, payload, and signature.

Spring Boot
String jwt = Jwts.builder()
  .setHeaderParam("typ", "[1]")
  .setSubject("[2]")
  .signWith(Keys.hmacShaKeyFor("[3]".getBytes()))
  .compact();
Drag options to blanks, or click blank then click option'
AJWT
Buser123
Cmysecretkeymysecretkeymysecretkeymysecretkey
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'token' instead of 'JWT' for header type.
Using short or invalid secret keys.
Putting literal words instead of actual username.