6

我有一个 servlet 和一个 HTML 页面。如何防止用户在注销后点击浏览器的后退按钮?我在 stackoverflow 中阅读了相同的问题,但答案是使用 java 脚本禁用浏览器历史记录或使用页面 - http 标头中没有缓存。我们如何使用防止返回操作的 servlet 来实现它,http-header no cache 是无用的,因为 Firefox 表示当再次刷新两次安全页面时页面已过期。

我以某种方式完成了示例方法,只是为了尝试(不是真实的)我的用户名和密码从 HTML 页面发布到 servlet,如果密码和用户名正确,则 servlet 将其存储在会话中。当再次请求安全页面时,如果会话存在,则显示安全页面,并且用户从会话中注销登录页面显示所有正在工作,除非用户点击浏览器的后退按钮注销失败。

我们如何防止安全 servlet 在注销后显示内容,然后在浏览器中按下返回按钮?

Welcome.html 的源代码

<html>
<body>

<form method="POST" action="Sessionexample">
<div align="center">
<table border="1"   style="border-collapse: collapse">
    <tr>
        <td>Username</td>
        <td><input type="text" name="username" size="20"></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="text" name="password" size="20"></td>
    </tr>
    <tr>
        <td height="24">&nbsp;</td>
        <td height="24">&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="Submit" name="B1"></td>
    </tr>
</table>
</div>
</form>
</body>
</html>

servlet 的 src

public class Sessionexample extends HttpServlet implements Servlet , Filter {
    private static final long serialVersionUID = 1L;
    public String username =null, password=null;
    public HttpSession session ;
    public PrintWriter pw;
    int do_get =0 ;
    /**
     * Default constructor. 
     */


    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        HttpSession session = request.getSession(false);         
        if (session == null || session.getAttribute("username") == null) {
            response.sendRedirect("welcome.html"); // No logged-in user found, so redirect to login page.
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            response.setDateHeader("Expires", 0);
        } else {
            chain.doFilter(req, res);  
        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        do_get=1;
        pw = response.getWriter();
        session=request.getSession(false);
        try
        {
            if(request.getParameter("action")!=null)
            {
                if(request.getParameter("action").equals("logout"))
                {

                    session = request.getSession(true);
                    session.setAttribute("username", "");
                    session.setAttribute("password", "");
                    session.invalidate();
                     response.sendRedirect("welcome.html");
                    return; 
                }
            }
            else
            if(session !=null)
                {
                 if( (String)session.getAttribute(username)!=null)
                username = (String)session.getAttribute("username").toString();
                if( (String)session.getAttribute("password") !=null)
                 password =session.getAttribute("password").toString();
                pw.write("not new-");
                serviced(request,response);
                }

        }
        catch(Exception ex)
        {
            pw.write("Error-"+ex.getMessage());
        } 

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        if(request.getParameter("username")!=null && request.getParameter("password")!=null )
        {
             username = request.getParameter("username").toString();
             password =  request.getParameter("password").toString(); 
        } 

        serviced(request,response);

    }


    protected void serviced(HttpServletRequest request, HttpServletResponse response) throws IOException
    {

            response.setContentType("text/html");

            pw = response.getWriter();  
        if( username !=null && password !=null)
            if( username.equals("admin") && password.equals("a"))
            {

                try
                {

                    if(do_get==0)
                    {
                session = request.getSession(true);
                session.setAttribute("username", "admin");
                session.setAttribute("password", "a");
                    }               
                pw.write("You are logged in : "+username+"  <br/> "+"<a href='?action=logout'><h1>   Logout </h1> </a>");

                }
                catch(Exception ex)
                {
                                        response.sendRedirect("welcome.html");

                }

            }
            else
            {
            response.sendRedirect("welcome.html");
            }
            else
                response.sendRedirect("welcome.html");
    }

    @Override
    public boolean accept(Object arg0) throws IOException {
        // TODO Auto-generated method stub
        return false;
    }       

}
4

1 回答 1

12

您的过滤器仅在受限页面上设置无缓存标题welcome.html,而不是在受限页面上。因此,每当浏览器通过后退按钮请求任何这些受限页面时,它都可能会显示缓存版本。您的过滤器需要在所有受限页面上设置无缓存标头。

所以,你需要改变

    if (session == null || session.getAttribute("username") == null) {
        response.sendRedirect("welcome.html"); // No logged-in user found, so redirect to login page.
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0);
    } else {
        chain.doFilter(req, res);  
    }

    if (session == null || session.getAttribute("username") == null) {
        response.sendRedirect("welcome.html"); // No logged-in user found, so redirect to login page.
    } else {
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0);
        chain.doFilter(req, res);  
    }
于 2013-01-09T15:32:55.803 回答