Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating a JWT (using the jose4j library - https://bitbucket.org/b_c/jose4j/wiki/Home)
//exception handling excluded to avoid verbosity
RsaJsonWebKey rsaJsonWebKey = RsaKeyProducer.produce();
JwtClaims claims = new JwtClaims();
claims.setSubject("user1");
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(rsaJsonWebKey.getPrivateKey());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
String jwt = jws.getCompactSerialization();
//the encoded JWT
eyJhbGciOiJSUzI1NiJ9
.eyJzdWIiOiJ1c2VyMSJ9
.HG9GCQPuC6w6pulbYE2uurCzpEwoWvz_8Ps5ZjgtfomyY4LWacDEzlHLnyMj9H7aqgcePC7_4l2wDXQV-S0BQRsIZfJeUUmWxlTlLzvKZr_2eEx00YZPPFZNoFCfwB-ajLHLLenROy4aSjPo_Vg9o7N-p0DZ1yZQoJhkvoVJgkhX9FeAf65kIZkbuJC9dmVkzXSOpVf4GZeCpNDJJYSo6IAnL3UEoWek6V9BtWgV-a4xvydp7vxkdDXmzmalGLYuWbuVG7rWcbWwSfsg38iEG-mqptqA_Kzk1VmjwWNo_BfvLuzjzuosqi732-5SRzBP-2zqGghBqMYsGgkqkH2n7A
//human readable format
{
"alg": "RS256" //header
}
{
"sub": "user1" //claim payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment