How to configure Spring to not set the Pragma No-Cache parameter

My Spring system is based on MVC, and I checked that Spring automatically installs PRAGMA: no-cache . The system is available to users through SSL. When users try to download something using INTERNET EXPLORER 7 or 8, the error message โ€œInternet Explorer cannot download the file from the serverโ€ appears (for more details: http://support.microsoft.com/default.aspx?scid=KB; EN-US; q316431 & ).

I tried to configure WebContentInterceptor as the code below, but does not work:

<mvc:interceptors> <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> <property name="cacheSeconds" value="2100" /> <property name="useExpiresHeader" value="false" /> <property name="useCacheControlHeader" value="false" /> <property name="useCacheControlNoStore" value="false" /> </bean> </mvc:interceptors> 

What can I avoid Spring send Pragma: no-cache and related to Cache Control?

Hello!

+8
java spring java-ee spring-mvc
source share
4 answers

You can write your own interceptor and set header values โ€‹โ€‹for the response object. Interceptors are nothing more than filters, so redefine the filter and use

prehandle and posthandle for setting request and response headers, respectively.

Let me know if you want specific examples to do this.

 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <beans:bean id="customInterceptor" class="org.example.interceptors.CustomInterceptor"> </beans:bean> </mvc:interceptor> </mvc:interceptors> public class CustomInterceptor implements HandlerInterceptor{ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView modelAndView) throws Exception { response.setHeader(...);} } 
+7
source share

Try setting cache seconds to a negative value.


If this does not help, you need to override:

  protected final void preventCaching(HttpServletResponse response) 

or

  protected final void applyCacheSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) 

Both methods are implemented in WebContentGenerator

+2
source share

The easiest approach is to simply stop the header entry using the servlet filter. Thus, the configuration of no Spring must be changed, and you will get the correct cache functionality for free.

 public class PragmaFilter implements Filter { private static String PRAGMA_HEADER = "Pragma"; @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, new NoPragmaHttpServletResponseWrapper(response)); } @Override public void destroy() { } private final class NoPragmaHttpServletResponseWrapper extends HttpServletResponseWrapper { private NoPragmaHttpServletResponseWrapper(ServletResponse response) { super((HttpServletResponse) response); } @Override public void addHeader(String name, String value) { if (PRAGMA_HEADER.equals(name)) { return; } super.addHeader(name, value); } @Override public void setHeader(String name, String value) { if (PRAGMA_HEADER.equals(name)) { return; } super.setHeader(name, value); } } } 
+2
source share
 <mvc:interceptors> <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor"> <property name="cacheSeconds" value="2100" /> <property name="useExpiresHeader" value="false" /> <property name="useCacheControlHeader" value="false" /> <property name="useCacheControlNoStore" value="false" /> <property name="cacheMappings"> <props> <prop key="/**/**">-1</prop><!-- removes pragma no-cache --> </props> </property> </bean> </mvc:interceptors> 
+1
source share

All Articles