0

我有两个用户。公开和登录。当用户是公开的并且会话超时时,我只需传递请求,例如

Cookie userCookie = getCookie(httpServletRequest, "userCookie");
if (userCookie != null) {
    String value = userCookie.getValue();
    if (value.equalsIgnoreCase("normal")) {
        session.setAttribute("logedin", "0");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    } else if (value.equalsIgnoreCase("loginUser")) {
        Cookie sessionCookie = getCookie(httpServletRequest, "WITSessionCookie");
        if (sessionCookie == null) {   //user 30 min timeout
            session.setAttribute("logedin", "0");
            Cookie changeUserCookie = new Cookie("userCookie", "normal");
            changeUserCookie.setPath("/");
            httpServletResponse.addCookie(changeUserCookie);

            //If user generate ajax request
            if ("partial/ajax".equals(httpServletRequest.getHeader("Faces-Request"))) {
                httpServletResponse.setContentType("text/xml");
            httpServletResponse.getWriter().append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", timeoutPage);

            } else {
                httpServletResponse.sendRedirect(timeoutPage);
            }
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        } else {  //User was login but its session timeout not expire
            session.setAttribute("logedin", "0");
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        }
    }
} //end of if (userCookie != null)

首先,我在 web.xml 文件中使用了这些行

<session-config>
    <session-timeout>
       10
    </session-timeout>
</session-config>
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/SessionExpire.xhtml</location>
</error-page>

如果我保留该选项,那么当会话超时时,我会看到您的会话过期的消息。我不想要,因为公共用户与会话无关。

if (value.equalsIgnoreCase("normal")) {
    session.setAttribute("logedin", "0");
    filterChain.doFilter(httpServletRequest, httpServletResponse);
}

但是,如果我注释掉该选项,那么我会得到 ViewExpiredException。喜欢

 <!-- 
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/faces/SessionExpire.xhtml</location>
</error-page>
-->
  WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.application.ViewExpiredException: viewId:/index.xhtml - View /index.xhtml could not be restored.
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:212)

所以我想问是否有任何方法可以在普通用户的情况下选项不起作用并简单地传递请求并且在登录用户选项工作的情况下意味着将用户重定向到会话过期消息。谢谢

4

0 回答 0