Set setCachePeriod for static resources in spring boot

In my opinion, it's better to use spring.resources.cache-period property to set the cache period of default Boot Resource Handler. So add the following to your application.properties:

spring.resources.cache-period = 31536000

And delete the BaseMvcConfig config file.


If you want to use spring security for controllers and setup cache for static content then you might want to configure exceptions in your WebSecurityConfigurerAdapter and set cache period in application.properties:

@Override 
public void configure(WebSecurity web) throws Exception { 
   web.ignoring().antMatchers("/js/**", "/css/**");
} 

#1 week cache
spring.resources.cache-period = 604800

Since spring.resources.cache-period is deprecated, you may want to use the newer spring.web.resources.cache.period instead, which takes either seconds (as before), or a Duration specification like this:

spring.web.resources.cache.period = P30D

See Duration#parse() JavaDoc for reference.