java - 如何使用 Spring 4 convertPropertyValue(用于读取加密值)?

标签 java spring spring-4

我们正在使用 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/

相关文章:

java - 为什么 graphics.drawString() 不重绘?

java - N/(2N - 1) 的递归求和

java - spring-data-jpa 的查询注解

java - 导入 org.springframework.data 无法在 gradle 中解析

spring-mvc - 我可以在Spring 4 MVC项目中使用Stylus和/或Jade吗?

Spring 4 无法执行 Java 8 默认方法

java - hibernate 条件来获取记录?

java - Websphere:公共(public)类加载器中的共享库比应用程序模块早于类路径,即使使用父级最后策略

java - Jaxb2Marshaller 使用空 namespace URI 创建 JAXBContext

spring - 配置两个@InitBinder 以使用相同的模型或实体但用于不同的@RequestMappings