2012-01-17 15 views
3

のGrails 1.3.7 +春・セキュリティ・コアプラグインは認証失敗閉鎖

AUTHFAIL閉鎖からログインフォームに入力されたパスワードを取得する方法はありで試行されたパスワードを取得しますか?

session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] 

でユーザー名を取得できますが、パスワードを取得する方法はありません。

session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY] 

は常にnullを返します。

答えて

0

最初に:しないでください。あなたがしたいことがあれば、それはセキュリティを破るでしょう。 パスワードをHTMLフォームのパスワードフィールドに書き込むことは、そのような悪いケースの1つです。 セッションにパスワードを保存することも良い考えではありません。

質問に答えるには:UsernamePasswordAuthenticationFilterを上書きし、カスタムクラスをSpringの設定で使用します。

tryAuthenticationの内容をコピーし、必要なものをセッションに追加します。

public class MyFilter extends UsernamePasswordAuthenticationFilter{ 
    public static final String SPRING_SECURITY_LAST_PASSWORD_KEY = "SPRING_SECURITY_LAST_PASSWORD"; 


    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { 
     if (postOnly && !request.getMethod().equals("POST")) { 
      throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); 
     } 

     String username = obtainUsername(request); 
     String password = obtainPassword(request); 

     if (username == null) { 
      username = ""; 
     } 

     if (password == null) { 
      password = ""; 
     } 

     username = username.trim(); 

     UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); 

     // Place the last username attempted into HttpSession for views 
     HttpSession session = request.getSession(false); 

     if (session != null || getAllowSessionCreation()) { 
      request.getSession().setAttribute(SPRING_SECURITY_LAST_USERNAME_KEY, TextEscapeUtils.escapeEntities(username)); 
      request.getSession().setAttribute(SPRING_SECURITY_LAST_PASSWORD_KEY, TextEscapeUtils.escapeEntities(password)); 
     } 

     // Allow subclasses to set the "details" property 
     setDetails(request, authRequest); 

     return this.getAuthenticationManager().authenticate(authRequest); 
    } 
} 
:あなたは

request.getSession().removeAttribute(SPRING_SECURITY_LAST_PASSWORD_KEY); 

Filterクラスが必要なものをやった後 セッションからパスワードを削除することは非常に賢明だろう

関連する問題