Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Encrypt Values from Properties File

I am currently using a UserDetailsService to get values from a user file:

<bean id="userDetailsService"  class="org.springframework.security.userdetails.memory.InMemoryDaoImpl">
<property name="userProperties" value="users.properties"/>
</bean>

My properties file is meant to be edited by the admin and the username passwords are not encrypted:

bob=bobpassword
alice=alicepassword

Now, since I use a PasswordEncoder in my application, I need to encrypt the passwords and add them to the UserDetails. This can be done somewhere in the code, but is not very handy in my opinion.

I found the PropertyPlaceholderConfigurer with the method convertPropertyValue(String value), which can be overridden.

From what I understand, it should be possible to load the properties file into the PropertyPlaceholderConfigurer, where the properties could be encrypted in the convertPropertyValue method and then loaded by the UserDetailsService. Is that possible to do? If yes, hints would help me, otherwise I'd appreciate to see an alternative solution.

like image 301
evgeni Avatar asked Jun 08 '26 22:06

evgeni


1 Answers

Take a look at Jasypt, it is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

You can see how to configure it with Spring here

As an alternative, you may also implement your own propertyPersister to do the (d)encryption:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
    <property name="propertiesPersister">
        <bean class="com.mycompany.MyPropertyPersister" />
    </property>        
</bean>

Take a look at the example here

like image 171
tolitius Avatar answered Jun 10 '26 18:06

tolitius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!