2009-07-16 9 views
1

私はログインページ用のリクエストスコープバッキングBeanを持っています(私はSpring Securityを使用しています)。認証エラーが発生した場合、Springはコンテキストに入れて、私のページにエラーメッセージを追加しようとしています。JSFのメソッドからメッセージを追加

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メソッドで、メソッドからエラーメッセージを追加する方法がいくつかありますか?

答えて

0

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

関連する問題