【问题标题】:How to identify if session has expired and current page如何识别会话是否已过期和当前页面
【发布时间】:2013-07-11 01:30:02
【问题描述】:

使用:Mojarra 2.15、jsf 2.0、Jboss 7.1

您好,我正在尝试在 doFilter 方法中实现会话已过期,但是:

  • 如果当前页面是 index.html 并且会话已过期,则让用户输入 username/pwd 并单击确定按钮并重定向到另一个 page.xhtml

问题是当用户键入用户名/密码和确定按钮时,我得到会话过期页面而不是转到正确的页面。

if (httpServletRequest.getRequestedSessionId() != null && !httpServletRequest.isRequestedSessionIdValid()) {
                session = httpServletRequest.getSession(true);
                session.setAttribute("logedin", "0");    // public user               
{

   if(httpServletRequest.getRequestURL().toString().contains("index.xhtml"))
   {
        httpServletResponse.sendRedirect(loginPage);
   }else
   {
         httpServletResponse.sendRedirect(timeoutPage);
   }
} else {
    request.setCharacterEncoding("UTF-8");
    chain.doFilter(httpServletRequest, httpServletResponse);
}

【问题讨论】:

    标签: java http jsf session servlets


    【解决方案1】:

    当您使用 JSF 时,我建议您使用 filter : Servlet Filter not working

    response.sendRedirect // 包含相对路径

    关于 do 过滤器:我假设您在登录方法中设置了 USER 对象属性

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws ServletException, IOException {
    
          HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            HttpSession session = request.getSession();
    
            if (session == null || session.getAttribute("User") == null) {
                  response.sendRedirect(request.getContextPath() + "/index.xhtml"); // No logged-in user found, so redirect to login page.
            } else {
                chain.doFilter(req, res); // Logged-in user found, so just continue request.
            }
    }
    

    休眠身份验证:我遇到了同样的问题,我通过检查会话是否仍然可用来解决它。然后当我想使用 DB 时,我只需要调用 getSessionFactory().createdelete ....

    protected Session getSessionFactory() { // Accessible que par les classes
                                            // filles
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        if(!session.getTransaction().isActive())
            session.beginTransaction();
        return session;
    }
    

    【讨论】:

    • 我在登录方法中设置了用户对象属性。但问题是当 currentpage 与 login.xhtml 页面不同时,我需要渲染 sessionexpiredpage.xhtml
    • 好的,你在使用休眠吗?
    • 是的。我通过休眠进行身份验证
    【解决方案2】:

    当用户对 index.html 的请求和会话过期时,您的过滤器会将其发送到登录表单。用户输入用户/密码并单击登录按钮。新请求发送到服务器。 您的过滤器检查条件。会话已过期过滤器一次又一次将用户发送到登录表单。
    在过滤器中,您最常向您的条件添加条件。如果从 index.html 收到请求并且会话已过期,并且请求中没有设置任何用户/密码或用户/密码不正确,则 hoto 登录表单,否则转到 page.xhtml。

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      相关资源
      最近更新 更多