【发布时间】:2016-03-02 09:27:27
【问题描述】:
使用 Spring Security 4.0.2.RELEASE
对于使用 spring-security 框架的基本用户身份验证,我实现了 spring-security DaoAuthenticationProvider
当用户尝试使用正确的用户名、不正确的密码和用户的帐户已被锁定登录时,我预计 spring-security 身份验证模块会抛出 BadCredentialsException但它会抛出 LockedException
我的问题是
- 为什么 spring-security 正在处理用户以进行进一步的身份验证,而凭据特别是密码不正确?
- 即使用户的密码无效,在应用程序中显示“用户已锁定”的消息是否是一种好习惯?
- 如何为无效密码和锁定用户生成/捕获
BadCredentialsException?
任何帮助将不胜感激。 Authentication Provider 实现代码是
@Component("authenticationProvider")
public class LoginAuthenticationProvider extends DaoAuthenticationProvider {
@Autowired
UserDAO userDAO;
@Autowired
@Qualifier("userDetailsService")
@Override
public void setUserDetailsService(UserDetailsService userDetailsService) {
super.setUserDetailsService(userDetailsService);
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
try {
Authentication auth = super.authenticate(authentication);
// if reach here, means login success, else exception will be thrown
// reset the user attempts
userDAO.resetPasswordRetryAttempts(authentication.getName());
return auth;
} catch (BadCredentialsException ex) {
// invalid login, update user attempts
userDAO.updatePasswordRetryAttempts(authentication.getName(), PropertyUtils.getLoginAttemptsLimit());
throw ex;
} catch (LockedException ex) {
// this user is locked
throw ex;
} catch (AccountExpiredException ex) {
// this user is expired
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
}
【问题讨论】:
标签: java spring spring-security