Class ResultEncryptionHandler

java.lang.Object
cloud.opencode.base.web.crypto.ResultEncryptionHandler

public class ResultEncryptionHandler extends Object
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 Details

    • ResultEncryptionHandler

      public ResultEncryptionHandler(EncryptionKeyResolver keyResolver)
      Create handler with key resolver 使用密钥解析器创建处理器
      Parameters:
      keyResolver - the key resolver | 密钥解析器
  • Method Details

    • encrypt

      public <T> EncryptedResult encrypt(Result<T> result, EncryptResult annotation)
      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

      public <T> EncryptedResult encrypt(Result<T> result, String keyAlias, String algorithm)
      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 使用注解配置解密 EncryptedResult

      Verifies signature first, then decrypts. Throws OpenCryptoException if 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

      public boolean shouldEncrypt(EncryptResult annotation)
      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