【发布时间】:2014-02-25 18:59:29
【问题描述】:
尝试使用 Spring 安全性进行身份验证过程,但得到 Bad credentials 异常。这是我在 spring-security.xml 文件中定义的内容
<beans:bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
</beans:bean>
<beans:bean id="passwordEncoder"
class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
</beans:bean>
<authentication-manager id="customerAuthenticationManager">
<authentication-provider user-service-ref="customerDetailsService">
<password-encoder hash="sha" />
</authentication-provider>
</authentication-manager>
customerDetailsService 是我们自己提供给 spring security 的实现。在我的 Java 代码中注册用户时,我在将密码添加到数据库之前对提供的密码进行编码
import org.springframework.security.authentication.encoding.PasswordEncoder;
@Autowired
private PasswordEncoder passwordEncoder;
customerModel.setPassword(passwordEncoder.encodePassword(customer.getPwd(), null));
当我尝试调试我的应用程序时,似乎 Spring 正在调用 AbstractUserDetailsAuthenticationProvider 身份验证方法,并且当它使用 additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication); 的 DaoAuthenticationProvider 执行附加身份验证检查时,它在以下点抛出错误凭据异常
if (authentication.getCredentials() == null) {
logger.debug("Authentication failed: no credentials provided");
throw new BadCredentialsException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails);
}
我不确定我在哪里做错了,需要在这里做什么才能把东西放在正确的地方,我已经检查了客户在数据库中是否存在并且密码是编码的。
【问题讨论】:
-
两个 id 相同的 bean?尝试使用
password-encoder的ref属性。 -
@AleksandrM:谢谢,这只是一个错字,我只是复制粘贴了两次:D
-
好的。你试过 ref 吗?
-
@AleksandrM:我告诉过你我的错字..我能够解决这个问题,这是由于从 ;) 传递了一些错误的值
-
实际上我的意思是密码编码器中的 ref。 :)
标签: spring security spring-mvc spring-security