0

j_usernamelogin.jsp に " " および " "と共にカスタム フィールドがj_passwordあり、ユーザーを認証する必要があります。次のように CustomUsernamePasswordAuthenticationFilter を使用してカスタム フィールドにアクセスしています。

public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
        String myCustomField= request.getParameter("myCustomField");
        request.getSession().setAttribute("CUSTOM_FIELD", myCustomField);

        return super.attemptAuthentication(request, response); 
    }
}

loadByUsernameUserDetailsS​​ervice クラスのメソッドでセッションにアクセスしようとしましたが、エラーが発生しました。カスタム UserDetailsS​​ervice のコードは次のとおりです。

public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException,  DataAccessException {

         ServletRequestAttributes attr = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
         HttpSession session = attr.getRequest().getSession();
        User userObject = dbObject.retrieveUser(userName,myCustomParameter)
// code here to retrieve my user from the DB using the userName and myCustomParameter that was retrieved from login.jsp and put in the session. Get the custom parameter from the session here.

         if (userObject == null)
              throw new UsernameNotFoundException("user not found");

         return new AuthenticationUserDetails(userObject);
    }

認証のためにこのカスタム パラメータにアクセスする方法はありますか? セッションを介して送信しても機能していないようです。

4

2 に答える 2

0

認証が行われた後にセッションが作成されませんか。したがって、attemptAuthentication への呼び出しの後に、新しい認証済みセッションが作成される可能性があります。

実装しているAbstractクラスの春のドキュメントは次のとおりです

http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.html#successfulAuthentication%28javax.servlet.http.HttpServletRequest,%20javax .servlet.http.HttpServletResponse,%20org.springframework.security.core.Authentication%29

loadByUsername が呼び出されるまでに、セッション属性が失われている可能性があります。

于 2012-05-31T18:12:33.930 に答える
0

私は正確な問題に遭遇しました。

RequestAttributes が現在のスレッドにバインドされていないことが問題のようです。機能させるには、現在のスレッドに明示的にバインドする必要がありました。

CustomUsernamePasswordAuthenticationFilter、ステートメントの後に request.getSession().setAttribute("CUSTOM_FIELD", myCustomField);

追加: RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));

これは私にとってはうまくいきました。

于 2013-11-01T08:56:52.093 に答える