Java 不使用jasypt解密application.properties中的密码
我发现SpringBoot2.0应用程序在application.properties中有加密属性,如下所示Java 不使用jasypt解密application.properties中的密码,java,spring,spring-boot,encryption,Java,Spring,Spring Boot,Encryption,我发现SpringBoot2.0应用程序在application.properties中有加密属性,如下所示 spring.jpa.hibernate.ddl-auto=none spring.jpa.database=mysql spring.datasource.url=mysql_url spring.datasource.username=username spring.datasource.password=ENC:encrypted_password spring.redis.ho
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.datasource.url=mysql_url
spring.datasource.username=username
spring.datasource.password=ENC:encrypted_password
spring.redis.host=111.1.1.1
spring.redis.port=18729
spring.redis.password=ENC:redis_encrypted_password
我有自己的应用程序自定义加密和解密机制,不想使用jasypt lib
现在我已经扩展了PropertyPlaceHolderConfigure,并进行了以下配置
/**
* Decrypt passwrods in properties if they are already decrypted.
* If property starts with ENC: then only decrypt and return the property value
*
*/
public class EncryptablePropertyPlaceholderConfigurer
extends PropertyPlaceholderConfigurer {
/**
* Decrypt password if its encrypted. If property starts with ENC: then its considered as encrypted.
* @param originalValue
* @return decrypted value if encrypted
*/
@Override
protected String convertPropertyValue(String originalValue) {
if(originalValue!=null && originalValue.trim().startsWith("ENC:")) {
String enc = originalValue.split("ENC:")[1];
System.out.println(originalValue.split("ENC:")[1]);
originalValue = MySecurityUtilsClass.decryptPassword(enc);
System.out.println(" Decrypted passwrod "+ originalValue);
}
return originalValue;
}
}
和配置
@Configuration
public class AppBeanConfig {
@Bean
public static PropertyPlaceholderConfigurer placeHolderConfigurerName() throws IOException {
PropertyPlaceholderConfigurer props = new EncryptablePropertyPlaceholderConfigurer();
props.setSystemPropertiesMode( PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER );
//add more properties as Required
props.setLocation(new PathMatchingResourcePatternResolver().getResource("classpath:/application.properties"));
return props;
}
}
调用了convertPropertyValue方法,但属性未解密,mysql连接因密码错误而失败
如果不使用jasypt lib,将提供正确的配置方式帮助
占位符配置名称()
方法。在@Bean
之后为占位符配置名称()
方法添加注释@Primary
后尝试运行程序。是否有人能提供一些帮助来打印解密的密码<代码>System.out.println(“解密密码”+原始值)代码>为什么你的bean是静态的?是的,它会打印解密的密码。有人能提供一些帮助吗?这行打印解密的密码吗<代码>System.out.println(“解密密码”+原始值)代码>为什么你的bean是静态的?是的,它会打印解密的密码