Class OpenJwt
java.lang.Object
cloud.opencode.base.crypto.OpenJwt
OpenJWT Facade - Simplified API for JWT operations
OpenJWT 门面类 - 简化的 JWT 操作 API
This facade provides easy-to-use static methods for common JWT operations, including creation and verification with various algorithms.
此门面类提供易于使用的静态方法用于常见的 JWT 操作,包括使用各种算法创建和验证。
Features | 主要功能:
- HMAC symmetric signing (HS256/384/512) - HMAC 对称签名
- RSA asymmetric signing (RS256/384/512) - RSA 非对称签名
- ECDSA asymmetric signing (ES256/384/512) - ECDSA 非对称签名
- Claim validation - 声明验证
- Expiration checking - 过期检查
Usage Examples | 使用示例:
// Quick JWT creation with HMAC
String token = OpenJwt.sign("user123", "secret-key", Duration.ofHours(1));
// Verify and get claims
JwtClaims claims = OpenJwt.verify(token, "secret-key");
// Create JWT with custom claims
JwtClaims customClaims = JwtClaims.builder()
.subject("user123")
.issuer("auth-service")
.expiresIn(Duration.ofHours(1))
.claim("role", "admin")
.build();
String customToken = OpenJwt.sign(customClaims, "secret-key");
// Create JWT with RSA
KeyPair keyPair = OpenJwt.generateRsaKeyPair();
String rsaToken = OpenJwt.signRsa("user123", keyPair.getPrivate(), Duration.ofHours(1));
JwtClaims rsaClaims = OpenJwt.verify(rsaToken, keyPair.getPublic());
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-crypto V1.2.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic JwtUtil.Builderbuilder()Creates a new JWT builder for advanced configuration.static JwtClaims.Builderclaims()Creates a new claims builder.static KeyPairGenerates an EC key pair for JWT signing.static KeyPairgenerateEcKeyPair(String curveName) Generates an EC key pair with specified curve.static KeyPairGenerates an RSA key pair for JWT signing.static KeyPairgenerateRsaKeyPair(int keySize) Generates an RSA key pair with specified key size.static JwtClaimsparseUnsafe(String token) Parses a JWT without verification (unsafe).static StringCreates a JWT with HMAC-SHA256 using JwtClaims.static Stringsign(JwtClaims claims, String secret, JwtAlgorithm algorithm) Creates a JWT with specified HMAC algorithm.static StringCreates a JWT with HMAC-SHA256.static StringsignEc(JwtClaims claims, PrivateKey privateKey) Creates a JWT with ECDSA-SHA256 using JwtClaims.static StringsignEc(JwtClaims claims, PrivateKey privateKey, JwtAlgorithm algorithm) Creates a JWT with specified ECDSA algorithm.static StringsignEc(String subject, PrivateKey privateKey, Duration expiration) Creates a JWT with ECDSA-SHA256.static StringsignRsa(JwtClaims claims, PrivateKey privateKey) Creates a JWT with RSA-SHA256 using JwtClaims.static StringsignRsa(JwtClaims claims, PrivateKey privateKey, JwtAlgorithm algorithm) Creates a JWT with specified RSA algorithm.static StringsignRsa(String subject, PrivateKey privateKey, Duration expiration) Creates a JWT with RSA-SHA256.static JwtClaimsVerifies a JWT with HMAC secret.static JwtClaimsVerifies a JWT with public key.
-
Method Details
-
sign
-
sign
-
sign
Creates a JWT with specified HMAC algorithm. 使用指定的 HMAC 算法创建 JWT。- Parameters:
claims- the JWT claimssecret- the secret keyalgorithm- the HMAC algorithm (HS256, HS384, HS512)- Returns:
- the JWT string
-
signRsa
Creates a JWT with RSA-SHA256. 使用 RSA-SHA256 创建 JWT。- Parameters:
subject- the subjectprivateKey- the RSA private keyexpiration- the expiration duration- Returns:
- the JWT string
-
signRsa
Creates a JWT with RSA-SHA256 using JwtClaims. 使用 JwtClaims 和 RSA-SHA256 创建 JWT。- Parameters:
claims- the JWT claimsprivateKey- the RSA private key- Returns:
- the JWT string
-
signRsa
Creates a JWT with specified RSA algorithm. 使用指定的 RSA 算法创建 JWT。- Parameters:
claims- the JWT claimsprivateKey- the RSA private keyalgorithm- the RSA algorithm (RS256, RS384, RS512, PS256, PS384, PS512)- Returns:
- the JWT string
-
signEc
Creates a JWT with ECDSA-SHA256. 使用 ECDSA-SHA256 创建 JWT。- Parameters:
subject- the subjectprivateKey- the EC private keyexpiration- the expiration duration- Returns:
- the JWT string
-
signEc
Creates a JWT with ECDSA-SHA256 using JwtClaims. 使用 JwtClaims 和 ECDSA-SHA256 创建 JWT。- Parameters:
claims- the JWT claimsprivateKey- the EC private key- Returns:
- the JWT string
-
signEc
Creates a JWT with specified ECDSA algorithm. 使用指定的 ECDSA 算法创建 JWT。- Parameters:
claims- the JWT claimsprivateKey- the EC private keyalgorithm- the ECDSA algorithm (ES256, ES384, ES512)- Returns:
- the JWT string
-
verify
-
verify
-
parseUnsafe
-
generateRsaKeyPair
Generates an RSA key pair for JWT signing. 生成用于 JWT 签名的 RSA 密钥对。- Returns:
- the RSA key pair
-
generateRsaKeyPair
Generates an RSA key pair with specified key size. 生成指定密钥大小的 RSA 密钥对。- Parameters:
keySize- the key size in bits- Returns:
- the RSA key pair
-
generateEcKeyPair
Generates an EC key pair for JWT signing. 生成用于 JWT 签名的 EC 密钥对。- Returns:
- the EC key pair (P-256)
-
generateEcKeyPair
-
builder
Creates a new JWT builder for advanced configuration. 创建新的 JWT 构建器用于高级配置。- Returns:
- a new JwtUtil.Builder instance
-
claims
Creates a new claims builder. 创建新的声明构建器。- Returns:
- a new JwtClaims.Builder instance
-