Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA AttributeConverter的上下文_Java_Jpa - Fatal编程技术网

Java JPA AttributeConverter的上下文

Java JPA AttributeConverter的上下文,java,jpa,Java,Jpa,我正在使用JPA AttributeConverter加密存储在数据库中的字符串。类似于在以下会议上提出的建议: 但是,该示例在转换器内部创建了密钥: @Converter public class CryptoConverter implements AttributeConverter<String, String> { private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; private static

我正在使用JPA AttributeConverter加密存储在数据库中的字符串。类似于在以下会议上提出的建议:

但是,该示例在转换器内部创建了密钥:

@Converter
public class CryptoConverter implements AttributeConverter<String, String> {

private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static final byte[] KEY = "MySuperSecretKey".getBytes();

@Override
public String convertToDatabaseColumn(String ccNumber) {
  // do some encryption
  Key key = new SecretKeySpec(KEY, "AES");
  try {
     Cipher c = Cipher.getInstance(ALGORITHM);
     c.init(Cipher.ENCRYPT_MODE, key);
     return Base64.encodeBytes(c.doFinal(ccNumber.getBytes()));
  } catch (Exception e) {
     throw new RuntimeException(e);
  }
}

@Override
public String convertToEntityAttribute(String dbData) {
  // do some decryption
  Key key = new SecretKeySpec(KEY, "AES");
  try {
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    return new String(c.doFinal(Base64.decode(dbData)));
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
}
@转换器
公共类CryptoConverter实现AttributeConverter{
私有静态最终字符串算法=“AES/ECB/PKCS5Padding”;
私有静态最终字节[]KEY=“MySuperSecretKey”.getBytes();
@凌驾
公共字符串convertToDatabaseColumn(字符串编号){
//做一些加密
Key Key=新的SecretKeySpec(Key,“AES”);
试一试{
Cipher c=Cipher.getInstance(算法);
c、 init(Cipher.ENCRYPT_模式,密钥);
返回Base64.encodeBytes(c.doFinal(ccNumber.getBytes());
}捕获(例外e){
抛出新的运行时异常(e);
}
}
@凌驾
公共字符串convertToEntityAttribute(字符串dbData){
//解密
Key Key=新的SecretKeySpec(Key,“AES”);
试一试{
Cipher c=Cipher.getInstance(算法);
c、 init(Cipher.DECRYPT_模式,密钥);
返回新字符串(c.doFinal(Base64.decode(dbData));
}捕获(例外e){
抛出新的运行时异常(e);
}
}
}
我的密钥(以及加密的其他部分)依赖于其他状态。此状态不是AttributeConverter接口的一部分,因此无法传入

存储此(每个请求)状态以便AttributeConverter可以访问它的最佳方式是什么


谢谢。

CDI可能会帮助您。向生产者提供所需的对象。请共享您的代码,以便我们查看以帮助您。