Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent user from traversing back using browser back button in spring `Web application`

I am developing web application using spring MVC frame work, I want to restrict users from traversing back using browsers back button whenever using my web application. how can I do so in springs? Is there any built in functionality in spring?

like image 222
Rajendra_Prasad Avatar asked Mar 28 '13 15:03

Rajendra_Prasad


1 Answers

Use following filter class in your application, don't forget to register this filter class in web.xml.

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class NoBrowserCacheFilter implements Filter{

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response=(HttpServletResponse)res;
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", -1);
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}

That's it, it solves your problem.

like image 194
Kishan_KP Avatar answered Oct 21 '22 06:10

Kishan_KP