⚠️Important security note: JVM has a critical vulnerability for ECDSA Algorithms - CVE-2022-21449. Please review the details of the vulnerability and update your environment.
If you're looking for an Android version of the JWT Decoder take a look at our JWTDecode.Android library.
You are viewing the documentation for the v4 beta release. For the latest stable release, please see the version 3.x documentation.
The library implements JWT Verification and Signing using the following algorithms:
JWS
Algorithm
Description
HS256
HMAC256
HMAC with SHA-256
HS384
HMAC384
HMAC with SHA-384
HS512
HMAC512
HMAC with SHA-512
RS256
RSA256
RSASSA-PKCS1-v1_5 with SHA-256
RS384
RSA384
RSASSA-PKCS1-v1_5 with SHA-384
RS512
RSA512
RSASSA-PKCS1-v1_5 with SHA-512
ES256
ECDSA256
ECDSA with curve P-256 and SHA-256
ES384
ECDSA384
ECDSA with curve P-384 and SHA-384
ES512
ECDSA512
ECDSA with curve P-521 and SHA-512
Note - Support for ECDSA with curve secp256k1 and SHA-256 (ES256K) has been dropped since it has been disabled in Java 15
Quickstart
Create and Sign a Token
You'll first need to create a JWTCreator instance by calling JWT.create(). Use the builder to define the custom Claims your token needs to have. Finally to get the String token call sign() and pass the Algorithm instance.
If a Claim couldn't be converted to JSON or the Key used in the signing process was invalid a JWTCreationException will raise.
Verify a Token
You'll first need to create a JWTVerifier instance by calling JWT.require() and passing the Algorithm instance. If you require the token to have specific Claim values, use the builder to define them. The instance returned by the method build() is reusable, so you can define it once and use it to verify different tokens. Finally call verifier.verify() passing the token.
If the token has an invalid signature or the Claim requirement is not met, a JWTVerificationException will raise.
Need to peek into a JWT without verifying it? (Click to expand)
Decode a Token
Warning: This will not verify whether the signature is valid. You should not use this for untrusted messages. You most likely want to use JWTVerifier as documented above instead.
If the token has an invalid syntax or the header or payload are not JSONs, a JWTDecodeException will raise.
Usage
Pick the Algorithm
The Algorithm defines how a token is signed and verified. It can be instantiated with the raw value of the secret in the case of HMAC algorithms, or the key pairs or KeyProvider in the case of RSA and ECDSA algorithms. Once created, the instance is reusable for token signing and verification operations.
When using RSA or ECDSA algorithms and you just need to sign JWTs you can avoid specifying a Public Key by passing a null value. The same can be done with the Private Key when you just need to verify JWTs.
Using static secrets or keys:
//HMACAlgorithmalgorithmHS = Algorithm.HMAC256("secret"); //use more secure key//RSARSAPublicKeypublicKey = //Get the key instanceRSAPrivateKeyprivateKey = //Get the key instanceAlgorithmalgorithmRS = Algorithm.RSA256(publicKey, privateKey);
Note: How you obtain or read keys is not in the scope of this library. For an example of how you might implement this, see this gist.
请发表评论