Class Jwt
java.lang.Object
com.codename1.security.Jwt
JSON Web Token (RFC 7519) signing and verification.
Supported algorithms:
HS256,HS384,HS512-- HMAC with SHA-2. Pure Java, available on every platform.RS256,RS384,RS512-- RSA-PKCS1-v1_5 with SHA-2. Backed by the platform's native crypto viaSignature.ES256,ES384,ES512-- ECDSA with SHA-2. Backed by the platform's native crypto viaSignature.none-- unsigned tokens. Accepted on the signing side only when caller explicitly passes it; rejected on verification unless caller opts in viaverifyAllowNoneAlgorithm.
Sign a token
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("sub", "user-123");
claims.put("exp", System.currentTimeMillis() / 1000 + 3600);
String token = Jwt.signHs256(claims, "secret".getBytes("UTF-8"));
Verify and read claims
Jwt parsed = Jwt.parse(token);
if (!parsed.verifyHs256("secret".getBytes("UTF-8"))) {
throw new SecurityException("bad signature");
}
String sub = (String) parsed.getClaim("sub");
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringECDSA with SHA-256 ("ES256")static final StringECDSA with SHA-384 ("ES384")static final StringECDSA with SHA-512 ("ES512")static final StringHMAC-SHA-256 ("HS256")static final StringHMAC-SHA-384 ("HS384")static final StringHMAC-SHA-512 ("HS512")static final StringUnsigned token marker -- verification rejects this unless the caller explicitly opts in.static final StringRSA PKCS#1 v1.5 with SHA-256 ("RS256")static final StringRSA PKCS#1 v1.5 with SHA-384 ("RS384")static final StringRSA PKCS#1 v1.5 with SHA-512 ("RS512") -
Method Summary
Modifier and TypeMethodDescriptionReturns thealgfield from the JWT header (e.g. "HS256").Returns the value of a single claim, or null if the claim is absent.Returns the parsed claims (token payload) as an unmodifiable view into the original map.Returns the parsed header as an unmodifiable view into the original map.byte[]Returns the raw bytes of the signature segment as decoded from URL-safe base64.static JwtParses an encoded JWT into aJwtobject.voidsetVerifyAllowNoneAlgorithm(boolean allow) static StringSignsclaimswith the given HMAC algorithm.static StringSignsclaimswith the given RSA or ECDSA algorithm.static StringSignsclaimswith HS256 and returns the encoded token.static StringSignsclaimswith HS384 and returns the encoded token.static StringSignsclaimswith HS512 and returns the encoded token.static StringBuilds an unsigned token (header{"alg":"none"}).booleanVerifies an RSA or ECDSA signature using the given public key.booleanverifyHs256(byte[] secret) Verifies with a shared HMAC secret.booleanverifyHs384(byte[] secret) HMAC verification with HS384.booleanverifyHs512(byte[] secret) HMAC verification with HS512.
-
Field Details
-
HS256
-
HS384
-
HS512
-
RS256
-
RS384
-
RS512
-
ES256
-
ES384
-
ES512
-
NONE
Unsigned token marker -- verification rejects this unless the caller explicitly opts in.- See Also:
-
-
Method Details
-
signHs256
-
signHs384
-
signHs512
-
sign
-
sign
-
signNone
-
parse
-
setVerifyAllowNoneAlgorithm
public void setVerifyAllowNoneAlgorithm(boolean allow) When set to true,verify(PublicKey)will accept tokens whosealgheader isnone(i.e. unsigned). The default is false because in most JWT deployments accepting unsigned tokens is a critical security bug. Only enable this if you have very deliberately decided that you trust the transport. -
verifyHs256
public boolean verifyHs256(byte[] secret) Verifies with a shared HMAC secret. The token'salgheader is read and must be one of the HS family. -
verifyHs384
public boolean verifyHs384(byte[] secret) HMAC verification with HS384. -
verifyHs512
public boolean verifyHs512(byte[] secret) HMAC verification with HS512. -
verify
Verifies an RSA or ECDSA signature using the given public key. The algorithm must match the token'salgheader (RS256/384/512 or ES256/384/512). -
getAlgorithm
Returns thealgfield from the JWT header (e.g. "HS256"). -
getHeader
-
getClaims
-
getClaim
-
getSignature
public byte[] getSignature()Returns the raw bytes of the signature segment as decoded from URL-safe base64. May be empty for unsigned tokens.
-