【问题标题】:Logout Servlet not working properly注销 Servlet 无法正常工作
【发布时间】:2014-05-12 12:09:30
【问题描述】:

我已经为注销 servlet 编写了这段代码,但是当我点击后退按钮时,一切正常。请建议

public class LogOut extends HttpServlet {


    private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
System.out.println("logging out");

HttpSession session1=request.getSession();  

session1.invalidate();  

out.print("You are successfully logged out!");  
RequestDispatcher rd= request.getRequestDispatcher("/Logout.jsp");
rd.forward(request, response);

out.close();  


}
}

【问题讨论】:

  • 会话与浏览器缓存无关。您需要禁用缓存才能更改后退按钮的行为。

标签: jakarta-ee servlets


【解决方案1】:
Session invalidation is enough for a perfect logout servlet.In order to made logout   working properly we want to add a concept of validating authentication.

HttpSession 会话 = request.getSession(true);

session.invalidate();

每次用户登录时创建一个 Auth 对象。包括 id、用户名..等。然后检查 authObj 是 servlet 内部每个操作的开始。会话失效后 如果您尝试执行任何操作,您将无法获得该 authObj。因为我们没有用户名 (例如:)我们已经保留在会话中。用户名不可用的原因是我们已经完成了会话失效。所以我们无法进行任何需要登录的操作。然后只需添加一个像这样的消息:

PrintWriter out = response.getWriter();

out.println("会话失败!");

【讨论】:

    【解决方案2】:

    虽然没有完美的方法(用户可以禁用 javascript),但我们可以使用 history.forward() 方法强制用户跳回下一页。但是由于浏览器不会每次都调用onload方法,而且还有其他问题,我们必须修改我们的代码

       <html>
        <head>
        <title>Page One</title>
        <script>
        function backButtonOverride()
        {
          setTimeout("backButtonOverrideBody()", 1);
        }
    
        function backButtonOverrideBody()
        {
    
          try {
            history.forward();
          } catch (e) {
    
          }
    
          setTimeout("backButtonOverrideBody()", 500);
        }
        </script>
        </head>
        <body onLoad="backButtonOverride()">
        <h1>Page One</h1>
        <a href="page2.html">Move to Page Two</a>
        </body>
        </html>
    

    我建议不要在注销页面之前的页面中实现此代码,而是在注销页面本身中实现此代码,并添加 javascript 代码以将用户从注销页面重定向到“登录页面”,只是为了增加安全性,使用:-

    window.location = url;
    

    【讨论】:

      【解决方案3】:

      登录服务

      你需要像这样做适当的

      response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0"); 
      response.addHeader("Pragma", "no-cache"); 
      response.addDateHeader ("Expires", 0);
      

      如果您使用 cookie,那么您也需要清除它们

      /**
               * Invalidate all cookies by, for each cookie received, 
               * overwriting value and instructing browser to deletes it
               */
              Cookie[] cookies = request.getCookies();
              if (cookies != null && cookies.length > 0) {
                  for (Cookie cookie : cookies) {
                      cookie.setValue("-");
                      cookie.setMaxAge(0);
                      response.addCookie(cookie);
                  }
              }
      

      在您的 Logout.jsp 中,您将这些添加到 head 标签中

      <head>
      <meta http-equiv="Cache-Control" content="no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0" />
      <meta http-equiv="Pragma" content="no-cache" />
      <meta http-equiv="Expires" content="0" />
      </head>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 2015-07-28
        • 2013-06-28
        • 2017-06-30
        • 1970-01-01
        相关资源
        最近更新 更多