在xml security-context.xml或数据库中存储密码的最佳做法?

时间:2013-07-30 13:19:39

标签: java database spring spring-security hashcode

美好的一天开发者。 我有问题。在我的Web应用程序中,我使用spring security。我有2个简单的角色:用户,管理员。对于这些规则中的每一个,我都有自己的密码,因为所有密码都可以访问我的网络应用程序。所以我现在将所有密码存储在security.xml中的sha-256哈希编码:

    <security:http pattern="/search" security="none" />

    <security:http auto-config="true" >

    <security:session-management session-fixation-protection="migrateSession"/>

    <security:intercept-url pattern="/input" access="ROLE_ADMIN, ROLE_USER"/>
    <security:intercept-url pattern="/delete" access="ROLE_ADMIN"/> 

    <security:form-login login-page="/login" 
                         authentication-failure-url="/loginfail"
                         default-target-url="/input" 
                         always-use-default-target="true" 
                         username-parameter="j_username"
                         password-parameter="j_password" />

    <security:logout logout-success-url="/logout"/>

        <security:session-management>
            <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
        </security:session-management>

    </security:http>


    <security:authentication-manager>
    <security:authentication-provider>
    <security:password-encoder hash="sha-256"/>
    <security:user-service>
    <security:user name="user" password="04f8996da763b7a969b1028ee3007569eaf3a635486ddab211d512c85b9df8fb" authorities="ROLE_USER"/>
    <security:user name="admin" password="8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918" authorities="ROLE_ADMIN"/>
    </security:user-service>
    </security:authentication-provider>
    </security:authentication-manager>

真的好主意吗?可能需要将它们存储在DB(例如H2)中以获得更多保护。

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- Using and configuring C3P0 proxy -->
        <property name="driverClass"><value>org.h2.Driver</value></property>
        <property name="jdbcUrl"><value>jdbc:h2:/home/vadim/workspace-sts-3.1.0.RELEASE/h2/EDUCATION</value></property>
        <property name="user"><value>sa</value></property>
        <property name="password" ><value></value></property>
        <property name="initialPoolSize"><value>3</value></property> <!-- Number of Connections a pool will try to acquire upon startup -->
        <property name="minPoolSize"><value>1</value></property> <!-- Minimum connection pool size -->
        <property name="maxPoolSize"><value>20</value></property> <!-- Max connection pool size -->
        <property name="maxConnectionAge"><value>3600</value></property> <!-- Set max connection age to 1 hour, after it will release -->
        <property name="maxIdleTime"><value>600</value></property> <!-- 10 minutes connection can stay unused before be discarded -->
        <property name="checkoutTimeout"><value>200000</value></property> <!-- Each what time check for unused connections -->
        </bean>

现在我的密码是空白的,但我应该有一个。如何保护它?

谢谢。

3 个答案:

答案 0 :(得分:1)

使用新的BCryptPasswordEncoder:

http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/org/springframework/security/crypto/bcrypt/BCryptPasswordEncoder.html

它会自动为您填写密码。

我推荐BCrypt,因为它很强大,速度慢,并且没有已知的弱点。 “慢度”实际上是您在散列算法中所需的功能,因为这意味着如果有人窃取您的密码需要更长的时间才能破解。

SHA 256是weakened。 MD5绝对是破碎的。

在xml文件中存储用户/散列密码的优点是简单。但是,您需要重新启动应用程序才能进行更改。此外,没有用户自我管理。

encode上有一个BCryptPasswordEncoder方法,可以让您对xml或数据库中存储的密码进行编码。

答案 1 :(得分:0)

当您存储密码时,您必须至少散列它(多次,例如1000次)并加盐。

哈希几次是必不可少的,因为如果有人试图忘记密码,他还需要像你一样多次哈希输入:这会让黑客失去时间。

Salting可防止黑客使用散列密码的反向表。

您还应该为每个用户使用不同的盐。

在这里,您的密码在配置文件中加密而没有任何盐,我只需使用反向表来查找相应的密码。

编辑:实际上,我试过了。您的第一个密码是“用户”。您的第二个密码是“ admin ”。我使用这个简单的公开reverse table获得了它们。

答案 2 :(得分:0)

没有两个用户硬编码到这两个角色。使用该应用程序的每个人都应该是拥有自己的用户名和密码的个人用户。个人用户将拥有一个或两个角色。

考虑到这一点,显然您需要将用户数据存储在数据库中。您的用户表应该包含用户名,哈希密码,盐和您需要的任何其他字段。

您应该有另一个包含角色的表,另一个表将用户映射到角色。

创建用户时,您应使用加密随机生成器(例如SecureRandom)生成salt。散列密码是使用散列算法创建的,salt,spring安全性具有ShaPasswordEncoder来执行此操作(它还支持多次散列迭代)。

我建议阅读spring security docs,特别是http://static.springsource.org/spring-security/site/docs/3.1.x/reference/ns-config.html#ns-auth-providers