Spring Encrypt 属性文件中的值 [英] Spring Encrypt Values from Properties File
问题描述
我目前正在使用 UserDetailsService
从用户文件中获取值:
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
现在,由于我在应用程序中使用了 PasswordEncoder
,我需要加密密码并将它们添加到 UserDetails
.这可以在代码的某个地方完成,但在我看来不是很方便.
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.
我找到了带有方法 convertPropertyValue(String value)
的 PropertyPlaceholderConfigurer
,该方法可以被覆盖.
I found the PropertyPlaceholderConfigurer
with the method convertPropertyValue(String value)
, which can be overridden.
据我所知,应该可以将属性文件加载到 PropertyPlaceholderConfigurer
中,其中的属性可以在 convertPropertyValue
方法中加密,然后由UserDetailsService
.那有可能吗?如果是,提示会对我有所帮助,否则我很乐意看到替代解决方案.
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.
推荐答案
看一看 Jasypt,它是一个 Java 库,允许开发人员以最少的努力为他/她的项目添加基本的加密功能,而无需深入了解密码学的工作原理.
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.
您可以在此处
作为替代方案,您也可以实现自己的 propertyPersister
来进行 (d) 加密:
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>
看一下例子 这里
这篇关于Spring Encrypt 属性文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!