【发布时间】:2015-07-08 18:08:09
【问题描述】:
我正在 Java EE 应用程序(Spring / Struts / Hibernate)中实现 Spring Security。我对我的客户 DaoAuthenticationProvider 有一些不满。
@Override
public void configure(AuthenticationManagerBuilder pAuth) throws Exception {
pAuth.authenticationProvider(mAuthenticationProvider)
.userDetailsService(mUserDetailsService)
.passwordEncoder(new Md5PasswordEncoder());
}
这是我的SecurityConfig(扩展WebSecurityConfigurerAdapter)课程。
当我调试应用程序时,我可以看到在我的自定义DaoAuthenticationProvider 中没有设置密码编码器(PlainTextPasswordEncoder 而不是 Md5),为什么?
之后我尝试在构造函数中手动设置这个值:
public LimitLoginAuthenticationProvider() {
setPasswordEncoder(new Md5PasswordEncoder());
setUserDetailsService(mUserDetailsService);
}
当我调试它时,我看到了正确的值。
但在这两种情况下,如果我这样做:
@Override
public Authentication authenticate(Authentication pAuthentication) {
Authentication lAuth = super.authenticate(pAuthentication);
return lAuth;
}
lAuth 的属性表示用户是否通过身份验证,无论密码是什么...
有人知道答案吗?
编辑:LimitLoginAuthenticationProvider 实现
@Component("authenticationProvider")
public class LimitLoginAuthenticationProvider extends DaoAuthenticationProvider {
@Autowired
private IUserDao mUserDao;
@Autowired
@Qualifier("userDetailsService")
UserDetailsService mUserDetailsService;
public LimitLoginAuthenticationProvider() {
setPasswordEncoder(new Md5PasswordEncoder());
}
@Autowired
@Qualifier("userDetailsService")
@Override
public void setUserDetailsService(UserDetailsService userDetailsService) {
super.setUserDetailsService(userDetailsService);
}
@Override
public Authentication authenticate(Authentication pAuthentication) {
Authentication lAuth = super.authenticate(pAuthentication);
return lAuth;
}
@Override
@Transactional(readOnly = true)
protected void additionalAuthenticationChecks(UserDetails pUserDetails,
UsernamePasswordAuthenticationToken pAuthentication)
throws AuthenticationException {
try {
User lUser = mUserDao.findUserByLogin(pAuthentication.getName());
if (lUser.getStatus() >= 3) {
logger.debug("User account is locked");
throw new LockedException(messages.getMessage(
"AbstractUserDetailsAuthenticationProvider.locked",
"User account is locked"));
}
} catch (DaoException e) {
}
}
}
【问题讨论】:
-
向您展示 LimitLoginAuthenticationProvider 实现
标签: java spring spring-security