Java Spring安全:加密密码
我使用的是spring 3.2.5。好吧,现在我正在使用哈希密码Java Spring安全:加密密码,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我使用的是spring 3.2.5。好吧,现在我正在使用哈希密码 MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); messageDigest.update(password.getBytes("UTF-8")); byte[] digestBytes = messageDigest.digest(); 我想使用spring提供的方法保护密码。我在网上搜索,大部
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.update(password.getBytes("UTF-8"));
byte[] digestBytes = messageDigest.digest();
我想使用spring提供的方法保护密码。我在网上搜索,大部分帖子都很旧。因此,任何示例都可以。我发现这是最简单的方法,因此这是我使用的方法:
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider
user-service-ref="userDetailsService">
<sec:password-encoder hash="sha-256">
<sec:salt-source user-property="username" />
</sec:password-encoder>
</sec:authentication-provider>
</sec:authentication-manager>
这里的要点是,
密码编码器
是在XML本身中定义的(如果需要,甚至是salt),因此不需要额外的代码。这是“Spring的方式”…您只需使用Spring安全性即可,
在spring-security.xml bean中添加以下行:
使用SHA-512
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="sha-512" />
</authentication-provider>
</authentication-manager>
如果您想通过电子邮件或手机号码等任何其他属性加载用户,则需要编写实现UserDetailsService
的自定义类,并在该类中编写实现
您可以使用org.springframework.security.crypto.password.StandardPasswordEncoder
类。这是一个非常少的麻烦,你不必担心盐和迭代-细节完全封装在编码器
<!-- password encoder -->
<beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder" />
<!-- This is the authentication manager -->
<authentication-manager>
<authentication-provider user-service-ref="authService">
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
站点了解更多信息。使用带有
属性的密码编码器(如其他答案所示)是正确的。我还想补充一点,在尝试使用旧的标准编码器时,建议使用的编码器是BCryptPasswordEncoder as Spring doc:
If you are developing a new system, BCryptPasswordEncoder is a better choice both in terms of security and interoperability with other languages.
您还可以阅读有关哈希的更多背景详细信息,其中BCrypt算法也是建议的算法之一。这不是加密。这是散列。我不知道你在问什么。我想用spring安全方法散列密码
If you are developing a new system, BCryptPasswordEncoder is a better choice both in terms of security and interoperability with other languages.