1

ログインページのリクエストスコープバッキングBeanがあります(Spring Securityを使用しています)。認証エラーが発生すると、Spring はそれをコンテキストに入れ、ページにエラー メッセージを追加しようとしています。

public void doLogin() throws IOException {
    final FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();
    externalContext.dispatch("/j_spring_security_check");

    if (externalContext.getRemoteUser() == null) {
        Exception loginError = (Exception) externalContext.getSessionMap().get(
                AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
        if (loginError instanceof BadCredentialsException) {
            externalContext.getSessionMap().put(
                    AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, null);
            context.addMessage("loginBtn", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid credentials!", null));
            context.responseComplete();
        }
    }

    context.renderResponse();
}

マイページ:

  <a4j:form prependId="false">
        <h:panelGrid columns="2" cellpadding="5" >
            <h:outputText value="#{msgs.login_username}"/>
            <h:inputText id="j_username"/>

            <h:outputText value="#{msgs.login_password}"/>
            <h:inputSecret id="j_password"/>

            <h:outputText value=" "/>
            <f:facet name="footer">
                <h:panelGroup>
                    <h:commandButton type="submit" id="loginBtn" action="#{guest.doLogin}" value="#{msgs.login}" />
                    <rich:spacer width="5" height="1"/>
                    <rich:message id="errorForLogin" for="loginBtn">
                        <f:facet name="errorMarker">
                            <h:graphicImage value="./resources/forbidden.gif"/>
                        </f:facet>
                    </rich:message>
                </h:panelGroup>
            </f:facet>
        </h:panelGrid>
        <br/><br/>
    </a4j:form>

しかし、それはうまくいきません。エラー表示コードを @PostConstruct メソッドに入れようとしていますが、これは役に立ちませんでした。これは PhaseListener を実装した場合にのみ機能しますが、リクエストごとに呼び出されるため、これは悪い考えだと思います。@PostConstruct メソッドで、メソッドからエラーメッセージを追加する方法がいくつかありますか?

4

1 に答える 1

0

反対側からアプローチできます。SpringSecurity を使用すると、AuthenticationProcessingFilter をサブクラス化し、onUnsuccessfulAuthentication メソッドをオーバーライドできます。そこに、目的の動作のコードを配置できます。次に、Spring Security の対応する Bean 定義で AuthenticationProcessingFilter の代わりにカスタム認証処理フィルター クラスを指定するだけです。

于 2009-07-24T11:34:27.870 に答える