2016-08-05 35 views
0

次の問題について少し辛抱しています。 JSF/PrimeFaces(Wildfly 10)では、AJAX呼び出しと非AJAX呼び出しの両方を使用しています。セッションが期限切れになり、誰かがシステムとやり取り(クリック)した場合は、login.xhtmlにリダイレクトされ、新しいログインが必要です。問題は、AJAX以外のコントロールの場合にのみ機能するということです。私は、AJAXコントロールをクリックすると、私は、ログインページに取得するが、私は、ログイン/パスワードを記入した場合、私は次のエラーを取得する:ページリダイレクトがAJAXコールの部分応答メッセージで失敗します

<partial-response id="j_id1"> 
    <changes> 
     <update id="j_id1:javax.faces.ViewState:0"> 
     <![CDATA[ 982009515771098472:-6468147585577406798 ]]> 
     </update> 
    </changes> 
</partial-response> 

私は、多くの資料を読み、以下のクラス実装しました:

JsfAjaxPhaseListener.java

public class JsfAjaxPhaseListener implements PhaseListener { 

    public void beforePhase(PhaseEvent event) { 
    } 

    public PhaseId getPhaseId() { 
     return PhaseId.RESTORE_VIEW; 
    } 

    @Override 
    public void afterPhase(PhaseEvent event) { 
     FacesContext context = FacesContext.getCurrentInstance(); 

     HttpServletRequest httpRequest = (HttpServletRequest) context.getExternalContext().getRequest(); 
     if (httpRequest.getRequestedSessionId() != null && !httpRequest.isRequestedSessionIdValid()) { 
      String facesRequestHeader = httpRequest.getHeader("Faces-Request"); 
      boolean isAjaxRequest = facesRequestHeader != null && facesRequestHeader.equals("partial/ajax"); 

      if (isAjaxRequest) { 
       try { 
        context.getExternalContext().redirect("/admin/login.xhtml"); 
       } catch (IOException ex) { 
        Logger.getLogger(JsfAjaxPhaseListener.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     } 
    } 
} 

SessionExceptionHandler.java

以下のような10
public class SessionExceptionHandler extends ExceptionHandlerWrapper { 

    private static final Logger LOGGER = LoggerFactory.getLogger(SessionExceptionHandler.class); 

    private ExceptionHandler wrapped; 

    SessionExceptionHandler(ExceptionHandler exception) { 
     this.wrapped = exception; 
    } 

    @Override 
    public ExceptionHandler getWrapped() { 
     return wrapped; 
    } 

    @Override 
    public void handle() throws FacesException { 
     final Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); 
     while (i.hasNext()) { 
      ExceptionQueuedEvent event = i.next(); 
      ExceptionQueuedEventContext context 
        = (ExceptionQueuedEventContext) event.getSource(); 

      Throwable t = context.getException(); 

      if (t instanceof ViewExpiredException) { 
       ViewExpiredException vee =(ViewExpiredException) t; 
       FacesContext fc =FacesContext.getCurrentInstance(); 
       Map<String, Object> requestMap = fc.getExternalContext().getRequestMap(); 
       NavigationHandler nav = 
         fc.getApplication().getNavigationHandler(); 
       try { 
        ((ConfigurableNavigationHandler)nav).performNavigation("admin/login.xhtml?faces-redirect=true"); 
        fc.renderResponse(); 

       } finally { 
        i.remove(); 
       } 
      } 
     } 
     getWrapped().handle(); 
    } 
} 

SessionExceptionHandlerFactory.java

public class SessionExceptionHandlerFactory extends ExceptionHandlerFactory { 

    private ExceptionHandlerFactory parent; 

    public SessionExceptionHandlerFactory(ExceptionHandlerFactory parent) { 
     this.parent = parent; 
    } 

    @Override 
    public ExceptionHandler getExceptionHandler() { 
     ExceptionHandler handler = new SessionExceptionHandler(parent.getExceptionHandler()); 
     return handler; 
    } 

} 

のweb.xmlが見えます:私たちは、通話のさまざまな修正を様々なリダイレクトを試してみました

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <context-param> 
     <param-name>facelets.SKIP_COMMENTS</param-name> 
     <param-value>true</param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      10 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>admin/index.xhtml</welcome-file> 
    </welcome-file-list> 
    <security-constraint> 
     <display-name>securityConstraint</display-name> 
     <web-resource-collection> 
      <web-resource-name>resources</web-resource-name> 
      <url-pattern>/admin/*</url-pattern> 
     </web-resource-collection> 
     <user-data-constraint> 
      <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
     </user-data-constraint> 
     <auth-constraint> 
      <role-name>Administrator</role-name> 
     </auth-constraint> 
    </security-constraint> 
    <login-config> 
     <auth-method>FORM</auth-method> 
     <realm-name>incomakerSecurityDomain</realm-name> 
     <form-login-config> 
      <form-login-page>/login.xhtml</form-login-page> 
      <form-error-page>/errorLogin.xhtml</form-error-page> 
     </form-login-config> 
    </login-config> 
    <security-role> 
     <role-name>Administrator</role-name> 
    </security-role> 
    <error-page> 
     <error-code>500</error-code> 
     <location>/errorCatchall.xhtml</location> 
    </error-page> 
</web-app> 

、その上の2つの完全な日を過ごしましたしかし、何も役に立たない。私が手動でログインページをリロードすると、すべてが機能します。プログラムでリロードしようとすると、部分的な応答で常に失敗します。

ありがとうございました。

+0

この情報は役に立ちましたか? http://stackoverflow.com/q/35841356 – BalusC

+0

ありがとう@BalusC。残念ながら、それは動作していないようです。まず、OmniFacesではなくPrimeFacesを使用しています。 context.getExternalContext()。redirect()の前にinvalidateSession()を追加しようとしましたが、成功しませんでした。 context.getExternalContext()。redirectの代わりにConfigurableNavigationHandler.performNavigation()を使うのが助けになると思われますが、私はなぜそれが解決策ではなく、回避策であると考えていません。 :-( – TomS

+1

OmniFacesはPrimeFacesのようなコンポーネントライブラリではありません。これは、JSFベースのアプリケーションで使用できるユーティリティライブラリです。 – BalusC

答えて

0

BalusCのおかげで、問題はOmniFacesをプロジェクトに追加することによって最終的に解決されました。次に、JsfAjaxPhaseListenerクラスとSessionExceptionHandlerクラスを削除しました。これらのクラスは不要になったため削除しました。 今は魅力のように動作します。

関連する問題