我们正在使用 spring 4 占位符功能
<context:property-placeholder location="classpath:/configs/*.properties,classpath:/configs/specific/*-config.properties" />
属性文件:
##sample.properties
user=admin
password=123
我们尝试加密属性文件中的密码。所以属性文件将是
##sample.properties
user=admin
password=ENC(RE%%$XC)
我发现 Spring 已经预报了这个。如 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyResourceConfigurer.html 中所述convertPropertyValue
方法记录为:
Convert the given property value from the properties source to the value which should be applied.
The default implementation simply returns the original value. Can be overridden in subclasses, for example to detect encrypted values and decrypt them accordingly.
但是我不知道怎么用?!我试图定义一个新的 bean:
<bean class="foo.bar.security.DecryptPropertyConfigurer">
<property name="locations">
<list>
<value>classpath:/configs/*.properties</value>
<value>classpath:/configs/bsi/*-config.properties</value>
</list>
</property>
</bean>
和
public class DecryptPropertyConfigurer extends PropertySourcesPlaceholderConfigurer {
@Override
protected String convertPropertyValue(String originalValue){
//if value is between ENC() then decrypt it
//return originalValue or decrypted value;
}
}
当我在 convertPropertyValue
设置断点时,它似乎从未被调用。
http://romiawasthy.blogspot.com/2012/02/encryptdecrpt-properties-in-spring.html包含一些很好的信息,但对我没有帮助。
最佳答案
这好像是spring的bug https://jira.spring.io/browse/SPR-8928 .
作为那里的一种评论解决方法,可以使用 doProcessProperties
as
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, final StringValueResolver valueResolver) {
super.doProcessProperties(beanFactoryToProcess,
new StringValueResolver() {
@Override
public String resolveStringValue(String strVal) {
return convertPropertyValue(valueResolver.resolveStringValue(strVal));
}
}
);
}
致谢 Michael Gallag
关于java - 如何使用 Spring 4 convertPropertyValue(用于读取加密值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570467/