【问题标题】:Spring Encrypt Values from Properties FileSpring 加密属性文件中的值
【发布时间】:2011-11-07 04:06:36
【问题描述】:

我目前正在使用UserDetailsService 从用户文件中获取值:

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

我的属性文件应由管理员编辑,并且用户名密码未加密:

bob=bobpassword
alice=alicepassword

现在,由于我在我的应用程序中使用了PasswordEncoder,我需要加密密码并将它们添加到UserDetails。这可以在代码中的某个地方完成,但在我看来不是很方便。

我找到了 PropertyPlaceholderConfigurerconvertPropertyValue(String value) 方法,它可以被覆盖。

据我了解,应该可以将属性文件加载到PropertyPlaceholderConfigurer 中,其中属性可以在convertPropertyValue 方法中加密,然后由UserDetailsService 加载。那有可能吗?如果是,提示会帮助我,否则我会很高兴看到替代解决方案。

【问题讨论】:

    标签: properties spring-security


    【解决方案1】:

    看看Jasypt,它是一个 java 库,允许开发人员以最小的努力为他/她的项目添加基本的加密功能,而无需深入了解密码学的工作原理。

    你可以看看如何用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>
    

    看看例子here

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 2015-12-29
      • 2011-04-15
      • 1970-01-01
      • 2015-09-30
      • 2012-07-23
      • 2015-09-10
      • 2017-03-11
      相关资源
      最近更新 更多