evgeni
evgeni

Reputation: 1755

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.

Upvotes: 3

Views: 9995

Answers (2)

tolitius
tolitius

Reputation: 22499

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

Upvotes: 2

Related Questions