【问题标题】:Bad credentials Exception | Spring Security错误的凭据异常 |春季安全
【发布时间】: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-encoderref 属性。
  • @AleksandrM:谢谢,这只是一个错字,我只是复制粘贴了两次:D
  • 好的。你试过 ref 吗?
  • @AleksandrM:我告诉过你我的错字..我能够解决这个问题,这是由于从 ;) 传递了一些错误的值
  • 实际上我的意思是密码编码器中的 ref。 :)

标签: spring security spring-mvc spring-security


【解决方案1】:

更改您的密码编码器声明

<password-encoder hash="sha" />

<password-encoder ref="passwordEncoder" />

检查您的 customerDetailsS​​ervice 是否从数据库加载用户凭据。 如果您仍然收到错误的凭据异常:将 erase-credentials="false" 添加到您的 authenticationManager。

<authentication-manager id="customerAuthenticationManager" erase-credentials="false">
   <authentication-provider user-service-ref="customerDetailsService">
     <password-encoder hash="sha" />
   </authentication-provider>
</authentication-manager>

【讨论】:

  • 我的客户详细信息服务正在从数据库中正确加载用户,问题是当additionalAuthenticationChecks 被调用时,我仍然会尝试根据您的建议进行更改,看看发生了什么
猜你喜欢
  • 2011-07-26
  • 2012-12-27
  • 2015-04-04
  • 2013-12-22
  • 1970-01-01
  • 2017-09-19
  • 2014-02-02
相关资源
最近更新 更多