title: How to enable HTTP caching for the resource handler with Spring MVC and Spring Security
body: <p>I wish to enable HTTP caching for some static resources such as images, for which access is restricted by Spring Security. (These resources are not security critical, but shouldn't be publicly accessible either). How do I avoid having Spring Security add HTTP response headers that disable caching?</p>
  
  <p>If I add <code>setCachePeriod()</code> into my resource handler registration in <code>WebMvcConfigurerAdapter.addResourceHandlers()</code> as following:</p>
  
  <pre><code>registry.addResourceHandler("/static/**")
    .addResourceLocations("classpath:/static/").setCachePeriod(3600);
  </code></pre>
  
  <p>The resources are still returned with following headers that disable caching:</p>
  
  <pre><code>Cache-Control: max-age=3600, must-revalidate
  Expires: Mon, 04 Aug 2014 07:45:36 GMT
  Pragma: no-cache
  </code></pre>
  
  <p>I want to avoid introducing any XML configuration into the project, which currently uses only Java annotation configuration.</p>
  
  <p>Are there better solutions than extending the Spring resource handler?</p>