2015-10-15 37 views
0

当我的会话在我的Java EE 7,JSF Web应用程序中过期时。在JSF 2中的AJAX请求中的ViewExpiredException上的重定向

我在ajax请求中得到ViewExpiredException。

我想重定向到一个页面,它向用户显示会话已过期。

我试过浏览谷歌和stackoverflow的解决方案,但我did not没有任何运气,让它与我想要它的工作。

UPDATE:

我尝试使用login_submit按钮时,该解决方案张贴@Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

它没有工作,在我的登录页面。

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:h="http://xmlns.jcp.org/jsf/html" 
    template="./../templates/login_template.xhtml"> 

    <ui:define name="title"> 
     <h:outputText value="#{bundle.login_title}"/> 
    </ui:define> 

    <ui:define name="content"> 
     <h:outputText escape="false" value="#{bundle.login_message}"/> 
     <h:form> 
      <h:panelGrid columns="2" cellpadding="5"> 
       <h:outputLabel for="username" value="#{bundle.login_username}"/> 
       <p:inputText id="username" type="text" value="#{authBean.username}" label="username"/> 
       <h:outputLabel for="password" value="#{bundle.login_password}"/> 
       <p:inputText id="password" type="password" value="#{authBean.password}" label="password"/> 
       <h:outputText value="#{bundle.login_invalid_password}" rendered="#{!authBean.validPassword and authBean.validUsername}"/> 
       <h:outputText value="#{bundle.login_invalid_username}" rendered="#{!authBean.validUsername}"/> 
       <p:commandButton value="#{bundle.login_submit}" action="#{authBean.doLogin}"/> 
      </h:panelGrid> 
     </h:form> 
    </ui:define> 

</ui:composition> 

不过,这并不与此JSF的XHTML页面是安全网页上工作,登录页面是一个公共页面。例如,下面的XHTML是安全网页上:

<p:commandButton styleClass="button #{chartBean.isViewButtonActive('MONTH')}" update="dataChart dataTable @form" value="#{bundle.performance_year}" action="#{chartBean.changeModel('MONTH')}" /> 

这确实一个AJAX POST请求,但被我的@WebFilter逮住。 (它也发生在Primefaces中的selectOneMenu)这个过滤器检查用户是否登录,如果没有登录,它将它们重定向到登录页面。但出于某种原因,使用上面给出的示例按钮,它也会被@WebFilter捕获,并且ajax请求将作为@WebFilter中指定的响应重定向获取。它不会被ExceptionHandler捕获。 @WebFilter仅适用于安全页面,请参见下文。

@WebFilter(
     filterName = "AuthorizationFilter", 
     urlPatterns = {"/secure/*"}, 
     dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.FORWARD} 
) 
public class AuthorizationFilter implements Filter { 

有没有人知道如何解决这个问题。

+0

见编辑答案。 –

回答

2

我不喜欢这样的web过滤,并能正常工作:

// Check if user logged in, if not redirect to login.xhtml 
    if (loginBean == null || !((LoginBean) loginBean).isLoggedIn()) { 
     boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With")); 

     if (!isAjax) { 
      res.sendRedirect(req.getContextPath() + "/login.xhtml"); 
     } else { 
      // Redirecting an ajax request has to be done in the following way: 
      // http://javaevangelist.blogspot.com/2013/01/jsf-2x-tip-of-day-ajax-redirection-from.html 
      String redirectURL = res.encodeRedirectURL(req.getContextPath() + "/login.xhtml"); 
      StringBuilder sb = new StringBuilder(); 
      sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><partial-response><redirect url=\"").append(redirectURL).append("\"></redirect></partial-response>"); 
      res.setCharacterEncoding("UTF-8"); 
      res.setContentType("text/xml"); 
      PrintWriter pw = response.getWriter(); 
      pw.println(sb.toString()); 
      pw.flush(); 
     } 
    } else { 
     // Let chain of filters continue; 
     chain.doFilter(request, response); 
    } 

Inspiration

+0

@Wesley Egbertsen对不起,这不是我真正的代码,现在发布了......你能否检查一下它是否也适合你?如果我没有记错,我遇到了问题,检测它是否是一个前面的代码的ajax请求 –

+1

我已经有一个自己的函数来检测它是否是一个Ajax请求,我真的接近一个解决方案,但是你的部分代码,你返回一个部分响应与重定向救了我! 这里是我如何检查它是否是一个AJAX请求: 'private boolean isAJAXRequest(ServletRequest request){ \t HttpServletRequest req =(HttpServletRequest)请求; \t返回req.getHeader( “面请求”)=空 \t \t \t && req.getHeader( “面-请求”)toLowerCase()的indexOf( “AJAX”)> -1!。; }' –