Open uniquejava opened 5 years ago
public class ReqInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
res.setHeader(HttpHeaders.PRAGMA, "no-cache");
res.setDateHeader(HttpHeaders.EXPIRES, 0);
return true;
}
@Override
public void postHandle
@Override
public void afterCompletion
}
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ReqInterceptor());
}
}
@Component
@Order(2)
public class NoCacheFilter implements Filter {
private final static Logger logger = LoggerFactory.getLogger(NoCacheFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
WebUtils.setNoCache(res);
chain.doFilter(request, res);
}
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new NoCacheFilter());
// List<String> urlPatterns = new ArrayList<String>();
// urlPatterns.add("*.js");
// urlPatterns.add("*.css");
// registrationBean.setUrlPatterns(urlPatterns);
// registrationBean.setOrder(1);
return registrationBean;
}
https://bedigit.com/blog/disable-browser-caching/
The correct minimum set of headers that works across the most important browsers:
Where:
For the Web Pages (HTML) add the following tags to the page(s) you want to keep browsers from caching (the code must be in the
section of your page, for example right after.htaccess (Apache)
PHP
Java Servlet
ASP
ASP.NET
Ruby on Rails
Python on Flask
Google Go
Resources