Rajendra_Prasad
Rajendra_Prasad

Reputation: 1304

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?

Upvotes: 12

Views: 6985

Answers (2)

Daniel James
Daniel James

Reputation: 13

You can use this simple code which will be a false back. It would reload the current page and intend to disable the back button on the browser. this in the intended jsp you want to block.

<script type="text/javascript">
    window.history.forward();
    function noBack() {
        window.history.forward();
    }
</script>

and add this to the body tag:

body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload=""

Upvotes: -1

Kishan_KP
Kishan_KP

Reputation: 4518

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.

Upvotes: 15

Related Questions