【问题标题】:Spring property placeholder decrypt resolved propertySpring属性占位符解密解析的属性
【发布时间】:2014-02-27 18:27:37
【问题描述】:

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

Jasypt 支持类似的东西,但实际上在尝试装饰 bean 之前解密所有属性值。

有什么想法或想法吗?

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

编辑所以正如我上面所说的 Jasypt 实现,以同样的方式进行拦截会让我得到正确的解密,我正在工作。我担心的是 - 属性集合在内存中保留多长时间,或者在占位符配置器使用结束后它们会消失吗?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    如果它以"{EAS}" 开头,您可以扩展PropertyPlaceholderConfigurer 并覆盖解密它的org.springframework.beans.factory.config.PropertyResourceConfigurer.convertPropertyValue(String) 方法。类似以下类的东西可以用作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
    }
    

    }

    您的上下文将 PropertyPlaceholder 声明为:

    <bean class="foo.bar.EncryptationAwarePropertyPlaceholderConfigurer">
        <property name="location">
            <value>my.properties</value>
        </property>
    </bean>
    

    您可以像这样简单地使用该属性:

    @Value("${encryptedMyProtectedValue}")
    private String decryptedValue;
    

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

    【讨论】:

    • 在这方面给你一个+1,因为我已经完成了那部分:-)。我正在编辑我的问题
    猜你喜欢
    • 1970-01-01
    • 2015-01-16
    • 2015-05-22
    • 2014-03-23
    • 2021-03-08
    • 2016-03-04
    • 2018-04-21
    • 2019-08-18
    • 2017-01-12
    相关资源
    最近更新 更多