JWT Structure Explained – Java Backend Security Interview
Fast recall
Quick revision
Core answer
iss, sub, aud, and exp; the signature lets a verifier detect changes and authenticate the issuer's key. Base64URL is encoding, not encryption. An encrypted JWT uses JWE and has a different five-part compact form.Interview-ready guide
Interview answer
A JWT is a standard token format for carrying JSON claims. The form most APIs use is a signed JWS with three Base64URL segments separated by dots. The protected header identifies the signing algorithm and may contain a key ID. The payload contains claims such as issuer, subject, audience, and expiry. The signature covers the encoded header and payload, so a verifier with the trusted key can detect any change.
Base64URL is only an encoding. Anyone who receives an ordinary signed JWT can read its header and payload, so passwords, private keys, and other secrets do not belong there. The signature supplies integrity and helps authenticate the issuer's key; it does not encrypt the claims.
Verification must include more than checking that a signature exists. The service should allow only its expected algorithms and keys, then validate issuer, audience, expiry, not-before, and any claims used for authorization. JWT can also be encrypted as JWE, whose compact form has five segments, so “three parts” specifically describes the common signed form.
JWT Structure Explained – Java Backend Security Interview
Direct Answer
A JWT (JSON Web Token) is a compact format for carrying claims between parties. The common signed form is a JWS with three Base64URL segments: protected header, payload, and signature. The header identifies the signing algorithm and often a key ID; the payload contains claims such as iss, sub, aud, and exp; the signature lets a verifier detect changes and authenticate the issuer's key. Base64URL is encoding, not encryption. An encrypted JWT uses JWE and has a different five-part compact form.
Code Example
import io.jsonwebtoken.Jwts;
import javax.crypto.SecretKey;
import java.util.Date;
SecretKey key = Jwts.SIG.HS256.key().build(); // demo: persist securely in a real service
Date now = new Date();
String token = Jwts.builder()
.issuer("https://auth.example")
.subject("user-42")
.audience().add("orders-api").and()
.issuedAt(now)
.expiration(new Date(now.getTime() + 10 * 60_000L))
.signWith(key)
.compact();
var claims = Jwts.parser()
.verifyWith(key)
.build()
.parseSignedClaims(token)
.getPayload();
Signature verification is only one check. A real verifier must also validate issuer, audience, time claims, and the expected algorithm for its trust boundary.
Common Mistakes & Pitfalls
- Treating the payload as encrypted: Freshers often think JWT hides the data because it looks encoded. The payload is only Base64URL-encoded — anyone can decode it and read the claims. Never put passwords, credit card numbers, or API keys in a JWT payload.
- Not validating the signature: Some implementations extract the payload without calling parseClaimsJws(), which skips signature verification. An attacker can then modify the token and the server will trust it. Always validate before using claims.
- Using weak or hardcoded secrets: Using "secret" or "12345" as the signing key makes JWTs trivially forgeable. In production, use a strong random key stored in environment variables, rotated regularly.
Check your understanding
Answer these without looking back at the article.
- What is the difference between HS256 and RS256 signing algorithms?
- What claims should you always include in a JWT?
- What happens if the JWT secret key is compromised?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.