0

LoginBean認証情報が有効かどうかを示す ajax コールバック パラメータを返す特定の を呼び出すログイン フォームがあります。コードは次のとおりです。

public void doLogin() {

    Authentication authenticationRequestToken =
             new UsernamePasswordAuthenticationToken(user, password);

    try {
        Authentication authenticationResponseToken =
                 authenticationManager.authenticate(authenticationRequestToken);

        SecurityContextHolder.getContext().
                     setAuthentication(authenticationResponseToken);

        if (authenticationResponseToken.isAuthenticated()) {
            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg;
            boolean loggedIn = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", user);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            context.addCallbackParam("loggedIn", loggedIn);
        }
    } .authenticate(...) catches ...

    // Here I need some code that continue whatever j_spring_security_check
    // would do after authenticating.
}

私のアプリケーションが現在動作している方法では、この への呼び出しの後doLogin()、フォームが に送信されj_spring_security_check、認証プロセスが再び行われ、前の作業が無駄になります。私はこれに対する解決策を見つけようとしています。

つまり、フィルターによってインターセプトされたときに何が起こるかをシミュレートするものj_spring_security_check(またはこのインターセプトを明示的に強制する方法) が必要なので、フォームが送信された後ではなく、ボタンの後ろで処理が行われます。

4

1 に答える 1

0

自分自身を使用するのではなく、春のセキュリティ認証 URL に転送するだけの方が良いでしょうSecurityContextHolder。このコードを見てください:

public String doLogin() throws ServletException, IOException {

    FacesContext context = FacesContext.getCurrentInstance();

        String springCheckUrl = this.buildSpringSecurityCheckUrl();

        HttpServletRequest request = (HttpServletRequest) context
                .getExternalContext().getRequest();

        RequestDispatcher dispatcher = request
                .getRequestDispatcher(springCheckUrl);

        dispatcher.forward((ServletRequest) request,
                (ServletResponse) context.getExternalContext.getResponse());

        context.responseComplete();

        return null;
    }

    private String buildSpringSecurityCheckUrl() {
        StringBuilder springCheckUrl = new StringBuilder(
                "/j_spring_security_check").append("?").append("j_username")
                .append("=").append(this.userName.trim()).append("&")
                .append("j_password").append("=")
                .append(this.userPassword.trim());
        return springCheckUrl.toString();
    }
}
于 2012-11-27T05:29:24.393 に答える