简体   繁体   中英

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.

for example <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:

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. 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. 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.

Directly from the documentation:

Allows for configuration of individual bean property values from a property resource, ie a properties file. Useful for custom config files ... that override bean properties configured in the application context.

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.

Just my 2 cents, of code snippets, which I have implemented, which can help someone, who may find similar situation.

<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 {

@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. 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

and Override doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, final StringValueResolver valueResolver)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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