繁体   English   中英

Spring Security 3.1.1-自定义登录成功问题

[英]Spring Security 3.1.1 - Customizing Login Success problems

我是Spring Security的新手,而且我一直在阅读API和Javadocs,并且我认为在此问题上我需要帮助。

到目前为止,基于反复试验,我观察到抛出异常会提示authenticate方法自动重定向到Login Failure Handler。 从那里开始,很容易重定向和自定义失败的身份验证流程。 但是,当成功登录时,除了HttpServletRequest,HttpServletResponse,Authentication对象之外,我似乎什么都没有传递给Login Success Handler。

我的登录成功有两种情况:

  1. 登录成功。

  2. 新用户登录应重定向到更改密码页面。

这里有一些问题:

  1. 在这种情况下可以调用request.setParameter(“ status”,“ FOR_CHANGE_PASSWORD”)吗? 安全吗?

  2. 我应该添加“ CHANGE_PASSWORD”权限吗? 这是好习惯吗?

我的问题是,我不想在LoginAuthenticator中调用userService方法,然后在我的Login Success Handler上再次调用它,只是为了检索用户的状态。 有什么解决方法吗?

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);

    } 


}

任何更多的建议将是巨大的。

一种典型的方法是在Authentication存储有关已登录用户的所有必要信息。

例如,诸如DaoAuthenticationProvider类的标准AuthenticationProvider在成功认证后构造Authentication对象时,将UserDetails的实现存储为principal ,并且应用程序开发人员可以提供自己的UserDetails子类,其中包括必要的信息。 您可以用相同的方式实现它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM