0

I am disabling the user to go back on the previous page after he has logged out. Controls comes in the filter but the page are still there in cache. I have used the filter suggested in this answer :

BalusC Answer

My filter looks like :

@WebFilter
public class NoCacheFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    System.err.println("Cache Filter- Called");
    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); // Proxies.

    chain.doFilter(req, res);
}

@Override
public void destroy() {
    // TODO Auto-generated method stub

}

web.xml looks like :

<filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>NoCacheFilter</filter-name>
        <filter-class>com.omnia.pie.cm.filters.NoCacheFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>NoCacheFilter</filter-name>
        <url-pattern>*.xhtml</url-pattern>
    </filter-mapping>

But still I am able to go back after I have logged out. Please help ,thanks in advance.

Talib
  • 1,134
  • 5
  • 31
  • 58
  • 1
    Is your filter getting called properly? Did you debug that to make sure, that each page is of type xhtml and filter is getting called properly or is there any other filter that is overriding these headers? Can you check response headers as well, when the page is accessed. – Optional Nov 09 '17 at 16:43
  • @Optional everything seems fine and it is being called properly. – Talib Nov 12 '17 at 05:55
  • @Optional do you think that I need to pass the 'response' in chain.doFilter instead of 'res' object ? – Talib Nov 12 '17 at 05:56
  • No. @talib both are same object so that shall not be issue. I was just wondering, when you do chain.dofilter, is there any other filter that is removing those headers. To test, go to chrome, f12, developer's tool will come. Access the page before logout. Check in developer's tool what was the response header. It should have got those cache header in response. If there is no such header, then something is wrong – Optional Nov 12 '17 at 07:55
  • @Optional thanks for the response, I have checked in the developer tools and the response header contains the following headers : Cache-Control no-cache, no-store, must-revalidate Pragma no-cache Expires Thu, 01 Jan 1970 00:00:00 GMT – Talib Nov 12 '17 at 08:33
  • @Optional any suggestion or idea ? – Talib Nov 12 '17 at 08:33

0 Answers0