简体   繁体   English

java spring - 覆盖从属性文件接收的属性值

[英]java spring - override property value received from property file

I have a scenario, where i get encoded password value from property file.我有一个场景,我从属性文件中获取编码的密码值。 I have own implementation of decryption, so want to decrypt the password from my java class and then want to use that decrypted value for further, where its referred.我有自己的解密实现,所以想从我的 java 类解密密码,然后想进一步使用解密的值,在那里引用。

for example <bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource" ... // more attributed set from properties like user, hostname etc. p:password="${myPropertey.password}" >例如<bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource" ... // more attributed set from properties like user, hostname etc. p:password="${myPropertey.password}" >

above code i need to implement something like as below, for specific to password attribute, as rest properties are fine, but password need to decrypt before it used.上面的代码我需要实现如下所示的内容,特定于密码属性,因为其余属性很好,但密码需要在使用前解密。

(below implementation is wrong, but just i have mentioned to give more and clear idea) (下面的实现是错误的,但我刚刚提到要给出更多更清晰的想法)

<bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource" ... // more properties p:password="myDecryptBean.decryptMyPassword(${myPropertey.password})" >

Basically, I need to decrypt password, which i get from property file, before it get used to establish database connection.基本上,在用于建立数据库连接之前,我需要解密从属性文件中获取的密码。

Thanks for your time and any help !!感谢您的时间和任何帮助!

You need to create a custom datasource object that decrypts the password 您需要创建一个解密密码的自定义数据源对象

<bean id="myDataSource" class="foo.bar.PasswordDecryptingDataSource"
... // more properties
p:encryptedPassword="${myPropertey.password}"
>

Implement foo.bar.PasswordDecryptingDataSource like so: 像这样实现foo.bar.PasswordDecryptingDataSource

public class PasswordDecryptingDataSource extends org.apache.common.dbcp.BasicDataSource {
    private myDecryptBean; // inject your decryption bean somehow...

    public void setEncryptedPassword(String password) {
        super.setPassword(myDecryptBean.decryptMyPassword(password))
    }
}

Since you are using Spring I would highly recommend looking into the PropertyResourceConfigurer class. 由于您使用的是Spring,我强烈建议您查看PropertyResourceConfigurer类。 See the Official Documentation . 请参阅官方文档

In particular take a look at the convertProperty(String propertyName, String propertyValue) method and consider creating a class that overrides it. 特别要看一下convertProperty(String propertyName, String propertyValue)方法,并考虑创建一个覆盖它的类。 Then you will be able to add in your custom decryption logic to the overriden method, which will be run every time Spring accesses that property. 然后,您将能够将自定义解密逻辑添加到overriden方法,该方法将在每次Spring访问该属性时运行。

Directly from the documentation: 直接来自文档:

Allows for configuration of individual bean property values from a property resource, ie a properties file. 允许从属性资源(即属性文件)配置各个bean属性值。 Useful for custom config files ... that override bean properties configured in the application context. 对自定义配置文件有用...覆盖在应用程序上下文中配置的bean属性。

Brian, Samuel 布莱恩,塞缪尔

Thanks a lot for your quick help, both approaches provide solution to my situation. 非常感谢您的快速帮助,这两种方法都为我的情况提供了解决方案。

I have implemented to extend PropertyPlaceholderConfigurer, considering in future also if any more property I may get as encoded, and don't want to limit the decryption logic upto DataSource. 我已经实现了扩展PropertyPlaceholderConfigurer,考虑到将来我是否可以获得更多的属性,并且不希望将解密逻辑限制到DataSource。

Just my 2 cents, of code snippets, which I have implemented, which can help someone, who may find similar situation. 只是我的2美分代码片段,我已经实现,这可以帮助可能会发现类似情况的人。

<bean class="com.foo.spring.util.PropertyUtil"> <property name="location"> <value>file:${foo.config.location}</value> </property> </bean>

following is class 以下是上课

`public class PropertyUtil extends PropertyPlaceholderConfigurer { `public class PropertyUtil extends PropertyPlaceholderConfigurer {

@Override
public String convertProperty(String propertyName, String propertyValue){
    return super.convertProperty(propertyName, decrypt(propertyValue));
}


private String decrypt(String){
// logical implementation
}

` `

I feel this thread is much helpful for me on this PropertyConvertValue.我觉得这个线程在这个 PropertyConvertValue 上对我很有帮助。 source of discussion 讨论来源

For quick reference: Here's the workaround I'm using, if anyone finds it helpful:供快速参考:这是我正在使用的解决方法,如果有人觉得它有帮助:

protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
                                   final StringValueResolver valueResolver) {

    super.doProcessProperties(beanFactoryToProcess,
            new StringValueResolver() {
                @Override
                public String resolveStringValue(String strVal) {
                    return convertPropertyValue(valueResolver.resolveStringValue(strVal));
                }
            }
    );
}




 @Override
protected String convertPropertyValue(String originalValue) {
    String value = decrypt(originalValue);
    return value;
}
 
private String decrypt(String value){
 //... write decryption logic here

}

Class CustomClass extends PropertySourcesPlaceholderConfigurer类 CustomClass 扩展了 PropertySourcesPlaceholderConfigurer

and Override doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, final StringValueResolver valueResolver)并覆盖 doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, final StringValueResolver valueResolver)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM