【发布时间】:2012-03-07 02:59:40
【问题描述】:
拥有带有密码编码的简单 Spring Security webapp:
<security:authentication-manager alias="authenticationManager">
<security:authentication-provider user-service-ref="personService">
<security:password-encoder hash="md5" ref="passwordEncoder">
<!-- <security:salt-source user-property="username"/> -->
</security:password-encoder>
</security:authentication-provider>
</security:authentication-manager>
编码也很简单:
person.setPassword(encoder.encodePassword(person.getPassword(), null));
所以在数据库中所有的密码都会被编码。 现在我想在应用程序中对具有特定用户名的某些用户进行身份验证。 之前(当密码是明文时)是这样的:
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
username, password);
Authentication authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
但现在我从 DB 获得编码密码,无法像以前那样进行身份验证。
问题。 Spring 不知道密码来自已经编码的 UsernamePasswordAuthenticationToken。他正在第二次对其进行编码。 谁能帮忙?
编辑
所以我在这里看到了两种解决方案:
- 实现自定义 DaoAuthenticationProvider 添加检查两个密码是否已经散列
- 实现自定义身份验证并将其手动放入安全上下文中。
还有其他人吗?什么是最好的?
【问题讨论】:
-
包含detail here 中的一些 SpringSecurity,可能对某些人有用。
标签: spring authentication spring-security md5