Decode JWT Header And Payload Claims

Split a token into three segments and Base64url-decode the header and payload so alg and claims can be inspected, without verifying the signature.

JavaAuth Jwt

Code

snippet
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class JwtPeek {

    // 演示用 HMAC 签名 JWT:header.payload.signature 三段
    static final String TOKEN =
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyLTQyIiwic2NvcGUiOiJyZWFkOm9yZGVycyIsImV4cCI6MTc2NDgwNjQwMH0.t_f-huzWVLm-EzPATQFhX_LhfU2nnftxIjXosF1nYN4";

    public static void main(String[] args) {
        String[] parts = TOKEN.split("\\.", -1);
        if (parts.length != 3) {
            throw new IllegalArgumentException("JWT 需要 header.payload.signature 三段");
        }

        Base64.Decoder decode = Base64.getUrlDecoder();
        String header = new String(decode.decode(parts[0]), StandardCharsets.UTF_8);
        String payload = new String(decode.decode(parts[1]), StandardCharsets.UTF_8);

        System.out.println("alg   = " + field(header, "alg"));
        System.out.println("sub   = " + field(payload, "sub"));
        System.out.println("scope = " + field(payload, "scope"));
        System.out.println("exp   = " + field(payload, "exp"));
        // 结构解码不是签名校验:claims 只代表声明,验证要另行走 HMAC/公钥
        System.out.println("signature verified = false");
    }

    // 极简扁平 JSON 取值:只适合本例的两层结构,生产解析请用 JSON 库
    static String field(String json, String key) {
        String needle = "\"" + key + "\":";
        int at = json.indexOf(needle);
        if (at < 0) {
            return null;
        }
        int valueStart = at + needle.length();
        if (json.charAt(valueStart) == '"') {
            int end = json.indexOf('"', valueStart + 1);
            return json.substring(valueStart + 1, end);
        }
        int end = valueStart;
        while (end < json.length() && ",}]".indexOf(json.charAt(end)) < 0) {
            end++;
        }
        return json.substring(valueStart, end);
    }
}

Decode JWT Header And Payload Claims: what runs and why

All of the logic lives in the class JwtPeek and the functions main and field.

The file repeats itself only inside while (end < json.length() && ",}]".indexOf(json.charAt(end)) < 0). Invalid states are rejected up front: it throws IllegalArgumentException instead of continuing with bad data.

What to watch for

  • Dependencies: none by construction — no registry, no lockfile line.

Using it

One signature anchors the file: public static void main(String[] args)

Labels you will see when it runs: alg =, sub =, scope =.

The sequence field lives in main; running the file triggers it.

Key statements

throw new IllegalArgumentException("JWT 需要 header.payload.signature 三段");
return json.substring(valueStart + 1, end);
return json.substring(valueStart, end);

Interactive Tools for This Topic

FAQ

How are precondition violations signaled?
`IllegalArgumentException` is raised at the guard, so invalid state never reaches the main logic.
What does Decode JWT Header And Payload Claims print when run?
The script runs standalone; its 5 labeled outputs include `alg =`, `sub =`, `scope =`.