Complete the code to create the JWT header with the correct algorithm.
Map<String, Object> header = new HashMap<>(); header.put("alg", "[1]");
The JWT header must specify the algorithm used for signing. "HS256" is the common HMAC SHA-256 algorithm used in JWT.
Complete the code to add the subject claim to the JWT payload.
Map<String, Object> payload = new HashMap<>(); payload.put("sub", "[1]");
The "sub" claim in JWT payload usually holds the username or user identifier string.
Fix the error in signing the JWT token with the secret key.
String token = Jwts.builder() .setHeader(header) .setClaims(payload) .signWith(Keys.hmacShaKeyFor("[1]".getBytes())) .compact();
The secret key for HS256 must be long enough (at least 256 bits). A short key like 'mysecret' is invalid and causes errors.
Fill both blanks to decode the JWT token and extract the payload claims.
Claims claims = Jwts.parserBuilder() .setSigningKey(Keys.hmacShaKeyFor("[1]".getBytes())) .build() .[2](token) .getBody();
To parse a signed JWT, use the long secret key and the method parseClaimsJws(token) to get the claims body.
Fill all three blanks to build a JWT token with header, payload, and signature.
String jwt = Jwts.builder() .setHeaderParam("typ", "[1]") .setSubject("[2]") .signWith(Keys.hmacShaKeyFor("[3]".getBytes())) .compact();
The header type is "JWT", the subject is the username like "user123", and the secret key must be long enough for signing.