Spring org.jasypt.exceptions.EncryptionOperationNotPossibleException
我正在使用Jasypt-1.9.0与Spring3.1和Hibernate4.0.1一起使用。我在应用程序中要求连接到数据库,该数据库的密码(root)以加密形式存储在应用程序的属性文件中 我在网上找到了以下链接: 我已针对我的需求完成了以下步骤和配置:Spring org.jasypt.exceptions.EncryptionOperationNotPossibleException,spring,hibernate,password-encryption,jasypt,Spring,Hibernate,Password Encryption,Jasypt,我正在使用Jasypt-1.9.0与Spring3.1和Hibernate4.0.1一起使用。我在应用程序中要求连接到数据库,该数据库的密码(root)以加密形式存储在应用程序的属性文件中 我在网上找到了以下链接: 我已针对我的需求完成了以下步骤和配置: 在构建路径中添加了jasypt-1.9.0和jasypt-hibernate4-1.9.0 在mydispatcher servlet文件中添加了以下内容: 现在,如果运行应用程序,将出现以下异常: org.jasypt.except
- 在构建路径中添加了jasypt-1.9.0和jasypt-hibernate4-1.9.0
- 在mydispatcher servlet文件中添加了以下内容:
org.jasypt.exceptions.EncryptionOperationNotPossibleException
at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:981)
at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:725)
at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
这个问题很可能已经过时了,但对于未来的求职者来说。。。 EncryptionOperationNotPossibleException是jasypt抛出的一般异常,用于屏蔽其他可能的异常。以下情况下可能会发生此异常:
- 您的jdk没有安装JCE unlimited Strength(最常见的情况)
- 数据库中的某些数据以前使用其他密码进行过加密
- 您在数据库中有一些以前未加密的数据,您在某些字段中添加了加密
- 由于某些奇怪的数据损坏,jasypt无法从db解密加密值
- 还有很多,你只需要调试一下,找出真正的原因
encryptor.setPassword(“jasypt”);//可以从web获取,环境变量。。。
encryptor.setAlgorithm(“PBEWithHMACSHA512AndAES_256”);
encryptor.setIvGenerator(新的RandomIvGenerator())代码>
解决方案:
encryptor.setPassword(“MYPAS_WORD”);//就像在CLI工具中一样
encryptor.setAlgorithm(“PBEWithHMACSHA512AndAES_256”)//去掉这个
encryptor.setIvGenerator(新的RandomIvGenerator())//同时删除此项
它会很好用的
在您的情况下,您可以删除algorithm属性,passwordEvnName需要与CLI工具中使用的密码匹配。如果在加密过程中未指定所有参数,Jasypt将使用默认值。确保在解密过程中使用这些精确的默认值。否则你可能会有麻烦
这是我的工作:
mvn jasypt:encrypt -Djasypt.encryptor.password='secret' \
-Djasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256 \
-Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator \
-Djasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator \
-Djasypt.encryptor.key-obtention-iterations=1000 \
-Djasypt.plugin.path='file:application.yml'
删除上述所有XML配置,并将以下bean添加到配置类中:
@Bean public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder =
DataSourceBuilder.create();
dataSourceBuilder.url(dbUrl);
dataSourceBuilder.username(username);
dataSourceBuilder.password(password);
return dataSourceBuilder.build();
}
从属性中添加值,如
@Value("${db.driverclassname}")
private String dbDriverClassName;
@Value("${db.url}")
private String dbUrl;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
并将这些值传递到数据源上方
在属性文件中配置加密密钥,如#
不要使用cmd和jaspyt jar创建加密密钥我将与您的密钥共享创建加密密钥的链接:
根据您的版本添加jaspyat依赖项
如果必须在服务器上运行,并且面临密码加密不匹配或不可能等问题,请再添加一个jdbc模板bean:
@Bean
public JdbcTemplate jdbcTemplate(DataSource
dataSourcee)
{
return new JdbcTemplate(dataSource);
}
它工作正常,未发现任何问题
使用该工具创建关键点。因为我曾多次尝试使用jaspyt命令行,但加密错误,不受支持。您可以将使用上述工具生成的密钥与密钥进行交叉检查
@Bean public DataSource dataSource() {
DataSourceBuilder dataSourceBuilder =
DataSourceBuilder.create();
dataSourceBuilder.url(dbUrl);
dataSourceBuilder.username(username);
dataSourceBuilder.password(password);
return dataSourceBuilder.build();
}
@Value("${db.driverclassname}")
private String dbDriverClassName;
@Value("${db.url}")
private String dbUrl;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
db.driverclassname=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/contactsdb
db.username=contactsuser
db.password=ENC(XcBjfjDDjxeyFBoaEPhG14wEzc6Ja+Xx
+hNPrJyQT888=
@Bean
public JdbcTemplate jdbcTemplate(DataSource
dataSourcee)
{
return new JdbcTemplate(dataSource);
}