简体   繁体   English

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

[英]Spring Security 3.1.1 - Customizing Login Success problems

I'm new to Spring Security and I've been reading the API and the Javadocs and I believe I need help regarding my issue. 我是Spring Security的新手,而且我一直在阅读API和Javadocs,并且我认为在此问题上我需要帮助。

So far, based from trial and error, what I've observed was throwing an exception prompts the authenticate method to automatically redirect to the Login Failure Handler. 到目前为止,基于反复试验,我观察到抛出异常会提示authenticate方法自动重定向到Login Failure Handler。 From there, it was easy redirecting and customizing the flow of the failed authentication. 从那里开始,很容易重定向和自定义失败的身份验证流程。 However, when it comes to logging in successfully, I can't seem to pass anything to the Login Success Handler other than the HttpServletRequest, HttpServletResponse, Authentication objects. 但是,当成功登录时,除了HttpServletRequest,HttpServletResponse,Authentication对象之外,我似乎什么都没有传递给Login Success Handler。

My login success has two scenarios: 我的登录成功有两种情况:

  1. Logging in successfully. 登录成功。

  2. New user logging in should be redirected to the change password page. 新用户登录应重定向到更改密码页面。

Here are some questions: 这里有一些问题:

  1. Is it okay to call request.setParameter("status", "FOR_CHANGE_PASSWORD") in this case? 在这种情况下可以调用request.setParameter(“ status”,“ FOR_CHANGE_PASSWORD”)吗? Is it safe? 安全吗?

  2. Should I add a "CHANGE_PASSWORD" authority? 我应该添加“ CHANGE_PASSWORD”权限吗? Is this good practice? 这是好习惯吗?

My problem is that I do not want to call the userService method in my LoginAuthenticator then call it again on my Login Success Handler just to retrieve the status of the user. 我的问题是,我不想在LoginAuthenticator中调用userService方法,然后在我的Login Success Handler上再次调用它,只是为了检索用户的状态。 Any work arounds? 有什么解决方法吗?

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

    } 


}

Any more suggestions would be great. 任何更多的建议将是巨大的。

A typical approach is to store all necessary information about the logged in user in its Authentication . 一种典型的方法是在Authentication存储有关已登录用户的所有必要信息。

For example, standard AuthenticationProvider s such as DaoAuthenticationProvider store an implementation of UserDetails as a principal when constructing Authentication object after successful authentication, and application developer can provide its own subclass of UserDetails that includes necessary information. 例如,诸如DaoAuthenticationProvider类的标准AuthenticationProvider在成功认证后构造Authentication对象时,将UserDetails的实现存储为principal ,并且应用程序开发人员可以提供自己的UserDetails子类,其中包括必要的信息。 You can implement it in the same way. 您可以用相同的方式实现它。

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

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