Spring Encrypt Values ​​from Properties File

I am currently using UserDetailsServiceto 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 intended for editing by the administrator, and user passwords are not encrypted:

bob=bobpassword
alice=alicepassword

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

I found PropertyPlaceholderConfigurerusing a method convertPropertyValue(String value)that can be overridden.

From what I understand, it should be possible to load the properties file into PropertyPlaceholderConfigurer, where the properties can be encrypted in the method convertPropertyValueand then loaded using UserDetailsService. Can this be done? If so, the tips would help me, otherwise I would appreciate an alternative solution.

+2
source share
2 answers

Take a look at Jasypt , this is a Java library that allows a developer to add basic encryption capabilities to their projects with minimal effort and without having to have in-depth knowledge of how cryptography works.

You can see how to configure it using Spring here

propertyPersister (d):

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

+2

Source: https://habr.com/ru/post/1609757/


All Articles