A JWT is made up of three dot-separated, Base64URL-encoded parts—header, payload, and signature—that together let a recipient verify who issued the token and what it claims, without needing to look anything up on the server.
A decoded JWT payload might look like {"sub": "jdoe", "roles": ["ADMIN"], "exp": 1728950400}, which a server verifies against the signature before trusting the claims.
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class JwtAnatomy {
static final Base64.Encoder B64 = Base64.getUrlEncoder().withoutPadding();
static String sign(String data, String secret) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
return B64.encodeToString(mac.doFinal(data.getBytes(StandardCharsets.UTF_8)));
}
public static void main(String[] args) throws Exception {
String header = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
String payload = "{\"sub\":\"jdoe\",\"roles\":[\"ADMIN\"],\"iss\":\"https://auth.example.com\","
+ "\"iat\":1793500000,\"exp\":1793503600}";
String secret = "demo-secret-key-at-least-32-bytes!!";
String unsigned = B64.encodeToString(header.getBytes(StandardCharsets.UTF_8)) + "."
+ B64.encodeToString(payload.getBytes(StandardCharsets.UTF_8));
String jwt = unsigned + "." + sign(unsigned, secret);
String[] parts = jwt.split("\\.");
System.out.println("Parts: " + parts.length);
System.out.println("Header: " + new String(Base64.getUrlDecoder().decode(parts[0])));
System.out.println("Payload: " + new String(Base64.getUrlDecoder().decode(parts[1])));
// Anyone can READ the payload; only the signature proves it wasn't changed
boolean valid = sign(parts[0] + "." + parts[1], secret).equals(parts[2]);
System.out.println("Signature valid: " + valid);
String tamperedPayload = B64.encodeToString(payload.replace("jdoe", "admin")
.getBytes(StandardCharsets.UTF_8));
boolean tamperedValid = sign(parts[0] + "." + tamperedPayload, secret).equals(parts[2]);
System.out.println("Tampered token valid: " + tamperedValid);
}
}Parts: 3
Header: {"alg":"HS256","typ":"JWT"}
Payload: {"sub":"jdoe","roles":["ADMIN"],"iss":"https://auth.example.com","iat":1793500000,"exp":1793503600}
Signature valid: true
Tampered token valid: false
A concise interview answer is
A JWT has three parts: a header describing the algorithm and token type, a payload holding claims like the subject, roles, and expiration, and a signature that verifies the token hasn't been tampered with. It's important to remember the payload is just encoded, not encrypted, so nothing sensitive should go in it unprotected.
Last updated: 1 November 2026
Previous question: How does OAuth2 authentication work?
Next question: What are the options for securing a REST API in Spring Boot?