如何使用Spring 4 convertPropertyValue(用于读取加密值)?
How to use Spring 4 convertPropertyValue (for reading encrypted values)?
我们正在使用 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 https://jira.spring.io/browse/SPR-8928 中的错误。
作为那里的一种评论解决方法,可以将 doProcessProperties
用作
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 致谢
我们正在使用 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 https://jira.spring.io/browse/SPR-8928 中的错误。
作为那里的一种评论解决方法,可以将 doProcessProperties
用作
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 致谢