您可以创建自定义身份验证管理器,您可以在其中检查用户是否已激活
<bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
p:authenticationManager-ref="customAuthenticationManager"
p:authenticationFailureHandler-ref="customAuthenticationFailureHandler"
p:authenticationSuccessHandler-ref="customAuthenticationSuccessHandler" />
和自定义 authenticationManager
<bean id="customAuthenticationManager"
class="com.mycompany.security.CustomAuthenticationManager" />
CustomAuthenticationManager.java
public class CustomAuthenticationManager implements import org.springframework.security.authentication.AuthenticationManager{
@Override
public Authentication authenticate(Authentication auth)
throws AuthenticationException {
User user = null;
if (auth.getName() == null) {
throw new BadCredentialsException("User does not exists!");
}
user = userService.getUserByUsername(auth.getName());
if (user == null) {
throw new BadCredentialsException("User does not exists!");
}
if (passwordEncoder.isPasswordValid(user.getPassword(), (String) auth.getCredentials(), null)) {
//check if user is activated if not throw appropriate excetion
} else {
throw new BadCredentialsException("User does not exists!");
}
}
它会将用户重定向回登录页面(如果配置正确)
现在在 login.jsp 中,通过
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
并向用户显示适当的消息}