使用基本字符串对象对MD5进行Spring安全性Salt配置
我查看了这个论坛和文档,但没有找到这个问题的答案,也就是说,如何使用基本java对象作为MD5编码的salt来进行基本的Spring安全配置设置 以下是我的Spring安全上下文代码段配置:使用基本字符串对象对MD5进行Spring安全性Salt配置,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我查看了这个论坛和文档,但没有找到这个问题的答案,也就是说,如何使用基本java对象作为MD5编码的salt来进行基本的Spring安全配置设置 以下是我的Spring安全上下文代码段配置: <beans:bean id="saltSource" class="com.myproject.sec.util.MyString" scope="singleton" > <beans:constructor-arg value="12345" /> <
<beans:bean id="saltSource" class="com.myproject.sec.util.MyString" scope="singleton" >
<beans:constructor-arg value="12345" />
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source ref="saltSource" />
</password-encoder>
</authentication-provider>
</authentication-manager>
…但此配置引发了一个不必要的错误异常,抱怨Salt源不属于org.springframework.security.authentication.dao.SaltSource接口,但我不想将用户详细信息属性用作我的Salt(因为此接口支持用户详细信息),而是如上所示的自定义字符串对象。我如何做到这一点
同样,作为第二个密切相关的问题,我知道我可以得到Salt作为用户名,如下所示:
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source user-property="username"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source system-wide="12345"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
和的全系统固定盐为“12345”,如下所示:
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source user-property="username"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source system-wide="12345"/>
</password-encoder>
</authentication-provider>
</authentication-manager>
…但是我如何将Salt作为用户名和系统范围常量“12345”的串联,例如,如果用户名是fred,则将Salt作为“fred12345”,而不使用重写来实现我自己的编码?请不要使用MD5散列密码,它很容易被破解 使用Spring Security最新的BCryptPasswordEncoder。它为您处理salt(并将hash和salt存储在同一db列中): 以下是我关于简单方法的答案:
如果您希望salt成为用户名+“12345”,您可以实现自己的SaltSource(很简单): 然后:
<beans:bean id="saltSource" class="com.myproject.UserNameAndStringSalt" scope="singleton" />
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userService">
<password-encoder hash="md5">
<salt-source ref="saltSource" />
</password-encoder>
</authentication-provider>
</authentication-manager>
建议使用spring security提供的标准密码编码器。它会自动为您处理盐渍。它还使用更强大的SHA256散列
谢谢您提供的信息。我将给这个答案打勾,但我是否可以问:(1)如果使用了唯一的连接盐(如上所述),为什么很容易破解MD5?(2) 如果我确实出于某些原因决定使用MD5,是否可以(不重写)通过Spring安全配置上下文文件将唯一的Salt(用户名+MyString对象)传递给MD5编码,如果可以,如何传递?下面是一些关于破解快速哈希的速度的数据:谢谢你的帖子,但我一直在寻找如何在不实现自己的Salt源代码的情况下实现这一点,但如果可能的话,可以通过Spring安全配置上下文文件来实现。使用BCryptPasswordEncoder的默认值为10轮,可以轻松设置为任何数字(例如1024)除了有一个用于散列的SecureRandom实例之外,还可以通过它的构造函数。它实现PasswordEncoder接口,该接口声明它将使用SHA-1或更大的哈希值与随机生成的8字节或更大的salt相结合。使用StandardPasswordEncoder时,具有1024次迭代的SHA-256哈希和8字节随机salt值以及一个秘密值。那么,BCryptPasswordEncoder使用什么哈希算法呢?这两个选项中哪一个更能抵御可能的黑客攻击?直到我发布答案后,我才注意到@neilmcguigan。仔细研究,BCryptEncoder是最好使用的(它甚至在StandardPasswordEncoder的源代码中这样说)。BCrypt不是一个散列算法,而是一个。它在盖子下面用河豚。我真正感兴趣的是bcrypt是自适应的。这意味着你可以在将来增加它的强度,使破解成本更高。谢谢你的澄清。