簡體   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