Spring属性占位符解密已解析的属性

Dav*_*e G 1 java spring

我想看看是否有一种方法可以拦截属性占位符机制,这样如果我有一个以某种方式标记为已加密的已解析属性值,我可以解密并将结果用作已解析的值.

Jasypt支持这样的东西但实际上在尝试装饰bean之前解密所有属性值.

有什么想法或想法吗?

我有自己的解密机制,并将值字符串标记为加密,{AES}作为编码值的前缀.

编辑所以正如我上面提到的关于Jasypt实现的那样,以相同的方式拦截会让我得到正确的解密,这是我工作的.我担心的是 - 在占位符配置器使用结束后,属性集合保留在内存中还是会消失多久?

Fra*_*eth 6

如果以它开头,您可以扩展PropertyPlaceholderConfigurer并覆盖org.springframework.beans.factory.config.PropertyResourceConfigurer.convertPropertyValue(String)解密它的方法"{EAS}".类似下面的类可以用作PropertyPlaceHolder:

package foo.bar;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptationAwarePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{

@Override
protected String convertPropertyValue(String originalValue) {
    if (originalValue.startsWith("{AES}")) {
        return decrypt(originalValue.substring(5));
    }
    return originalValue;
}

private String decrypt(String value) {
    return value.toLowerCase(); // here your decryption logic
}
Run Code Online (Sandbox Code Playgroud)

}

您的上下文将PropertyPlaceholder声明为:

<bean class="foo.bar.EncryptationAwarePropertyPlaceholderConfigurer">
    <property name="location">
        <value>my.properties</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

您将使用该属性如下:

@Value("${encryptedMyProtectedValue}")
private String decryptedValue;
Run Code Online (Sandbox Code Playgroud)

编辑:org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(ConfigurableListableBeanFactory)将基本上加载属性(到本地属性对象),转换和处理它们.通过调用进行处理org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(ConfigurableListableBeanFactory, Properties).在bean处理之后,使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurerProperties对象将不会保留在内存中.它基本上只用于在您的上下文中设置bean的属性,并将被处理.