Class ResultEncryptionHandler
java.lang.Object
cloud.opencode.base.web.crypto.ResultEncryptionHandler
Result Encryption Handler
响应加密处理器
Core handler that framework interceptors delegate to for encrypting/decrypting
Result objects. This class manages ResultEncryptor instances
and resolves encryption keys via EncryptionKeyResolver.
框架拦截器委托的核心处理器,用于加密/解密 Result 对象。
此类管理 ResultEncryptor 实例,并通过 EncryptionKeyResolver 解析加密密钥。
Usage Examples | 使用示例:
// Create handler
EncryptionKeyResolver keyResolver = alias -> loadKey(alias);
ResultEncryptionHandler handler = new ResultEncryptionHandler(keyResolver);
// In framework interceptor: encrypt response
EncryptResult annotation = method.getAnnotation(EncryptResult.class);
if (annotation != null && annotation.enabled()) {
EncryptedResult encrypted = handler.encrypt(result, annotation);
return encrypted;
}
// In framework interceptor: decrypt request
DecryptResult annotation = param.getAnnotation(DecryptResult.class);
if (annotation != null) {
Result<T> decrypted = handler.decrypt(encrypted, dataType, annotation);
}
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-web V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionResultEncryptionHandler(EncryptionKeyResolver keyResolver) Create handler with key resolver 使用密钥解析器创建处理器 -
Method Summary
Modifier and TypeMethodDescription<T> Result<T> decrypt(EncryptedResult encrypted, TypeReference<T> typeReference, DecryptResult annotation) Decrypt an EncryptedResult with TypeReference using annotation configuration 使用注解配置和 TypeReference 解密 EncryptedResult<T> Result<T> decrypt(EncryptedResult encrypted, TypeReference<T> typeReference, String keyAlias, String algorithm) Decrypt an EncryptedResult with TypeReference 使用 TypeReference 解密 EncryptedResult<T> Result<T> decrypt(EncryptedResult encrypted, Class<T> dataType, DecryptResult annotation) Decrypt an EncryptedResult using annotation configuration 使用注解配置解密 EncryptedResult<T> Result<T> decrypt(EncryptedResult encrypted, Class<T> dataType, String keyAlias, String algorithm) Decrypt an EncryptedResult with explicit key alias and algorithm 使用指定的密钥别名和算法解密 EncryptedResult<T> EncryptedResultencrypt(Result<T> result, EncryptResult annotation) Encrypt a Result using annotation configuration 使用注解配置加密 Result<T> EncryptedResultEncrypt a Result with explicit key alias and algorithm 使用指定的密钥别名和算法加密 ResultbooleanshouldEncrypt(EncryptResult annotation) Check if a Result should be encrypted based on annotation 根据注解判断 Result 是否需要加密
-
Constructor Details
-
ResultEncryptionHandler
Create handler with key resolver 使用密钥解析器创建处理器- Parameters:
keyResolver- the key resolver | 密钥解析器
-
-
Method Details
-
encrypt
Encrypt a Result using annotation configuration 使用注解配置加密 Result- Type Parameters:
T- the data type | 数据类型- Parameters:
result- the result to encrypt | 要加密的响应annotation- the encryption annotation | 加密注解- Returns:
- the encrypted result | 加密后的响应
-
encrypt
Encrypt a Result with explicit key alias and algorithm 使用指定的密钥别名和算法加密 Result- Type Parameters:
T- the data type | 数据类型- Parameters:
result- the result to encrypt | 要加密的响应keyAlias- the key alias | 密钥别名algorithm- the algorithm | 算法- Returns:
- the encrypted result | 加密后的响应
-
decrypt
public <T> Result<T> decrypt(EncryptedResult encrypted, Class<T> dataType, DecryptResult annotation) Decrypt an EncryptedResult using annotation configuration 使用注解配置解密 EncryptedResultVerifies signature first, then decrypts. Throws
OpenCryptoExceptionif signature verification fails.先验签再解密。验签失败抛出
OpenCryptoException。- Type Parameters:
T- the data type | 数据类型- Parameters:
encrypted- the encrypted result | 加密的响应dataType- the data type class | 数据类型类annotation- the decryption annotation | 解密注解- Returns:
- the decrypted result | 解密后的响应
-
decrypt
public <T> Result<T> decrypt(EncryptedResult encrypted, TypeReference<T> typeReference, DecryptResult annotation) Decrypt an EncryptedResult with TypeReference using annotation configuration 使用注解配置和 TypeReference 解密 EncryptedResult- Type Parameters:
T- the data type | 数据类型- Parameters:
encrypted- the encrypted result | 加密的响应typeReference- the type reference for generic types | 泛型类型引用annotation- the decryption annotation | 解密注解- Returns:
- the decrypted result | 解密后的响应
-
decrypt
public <T> Result<T> decrypt(EncryptedResult encrypted, Class<T> dataType, String keyAlias, String algorithm) Decrypt an EncryptedResult with explicit key alias and algorithm 使用指定的密钥别名和算法解密 EncryptedResult- Type Parameters:
T- the data type | 数据类型- Parameters:
encrypted- the encrypted result | 加密的响应dataType- the data type class | 数据类型类keyAlias- the key alias | 密钥别名algorithm- the algorithm | 算法- Returns:
- the decrypted result | 解密后的响应
-
decrypt
public <T> Result<T> decrypt(EncryptedResult encrypted, TypeReference<T> typeReference, String keyAlias, String algorithm) Decrypt an EncryptedResult with TypeReference 使用 TypeReference 解密 EncryptedResult- Type Parameters:
T- the data type | 数据类型- Parameters:
encrypted- the encrypted result | 加密的响应typeReference- the type reference | 类型引用keyAlias- the key alias | 密钥别名algorithm- the algorithm | 算法- Returns:
- the decrypted result | 解密后的响应
-
shouldEncrypt
Check if a Result should be encrypted based on annotation 根据注解判断 Result 是否需要加密- Parameters:
annotation- the annotation (may be null) | 注解(可能为null)- Returns:
- true if should encrypt | 如果需要加密返回true
-