我是Spring Security的新手,我一直在阅读API和Javadocs,我相信我需要关于我的问题的帮助。Spring Security 3.1.1 - 自定义登录成功问题
到目前为止,基于试验和错误,我所观察到的是抛出异常提示authenticate方法自动重定向到登录失败处理程序。从那里,很容易重定向和自定义失败的身份验证流程。但是,在成功登录时,除了HttpServletRequest,HttpServletResponse,Authentication对象之外,我似乎无法将任何内容传递给登录成功处理程序。
我登录成功有两种情况:
登录成功。
新用户登录应该被重定向到更改密码页面。
这里有一些问题:
是否可以调用request.setParameter( “状态”, “FOR_CHANGE_PASSWORD”)在这种情况下?它安全吗?
我应该添加一个“CHANGE_PASSWORD”权限吗?这是好的做法吗?
我的问题是,我不想叫userService方法在我LoginAuthenticator然后再次调用它在我登录成功处理程序只是为了获取用户的状态。任何解决方法?
public class LoginAuthenticator implements AuthenticationProvider{
private static final Logger log = LoggerFactory.getLogger(LoginAuthenticator.class);
private static final List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
@Autowired
UserService userService;
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
log.info("Authenticating...");
log.debug("Username: {}" , authentication.getPrincipal());
log.debug("Password: {}" , (String) authentication.getCredentials());
WSResponse response = userService.authenticateLogin(username, password);
//User log-in failure
if(response.getResponseCode != 200){
if(response.getResponseStatus.equals("BAD CREDENTIALS")){
throw new BadCredentialsException("Bad Credentials");
}
else{
throw new AccountStatusException("Account is locked")
}
}
else{
log.info("User credentials are valid... logging in");
AUTHORITIES.add(new SimpleGrantedAuthority("USER"));
return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), (String) authentication.getCredentials(), AUTHORITIES);
}
}
更多的建议将是伟大的。
起初有点难以吸收,但最终使它工作。甜! – mpmp