Sample code for 30+ languages & platforms
Java

Get a JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetProtectedH, which copies the decoded protected header of a signature into a JsonObject.

Background. The protected header is integrity-protected by the signature. Inspecting it (for example the alg value) is useful before deciding how to validate.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Jws;

public class ChilkatExample {

    public static void main(String[] args) {
        Jws jws = new Jws();

        // Load the JWS compact serialization.
        String jwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
        try {
            jws.loadJws(jwsCompact);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Copy the decoded protected header of signature 0 into a JsonObject.
        JsonObject json = new JsonObject();
        try {
            jws.getProtectedH(0, json);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        json.setEmitCompact(false);
        System.out.println(json.emit());
    }
}