Using spring boot 2.1.1 and additionally spring security 5.1.1.
1. For resources using resourcehandler in code (UNTESTED):
You can add customized extensions of resources this way.
registry.addResourceHandler
Is for adding the uri path where to get the resource/s
.addResourceLocations
Is for setting the location in the filesystem where the resources are located(
given is a relative with classpath
.setCacheControl
.setCachePeriod
Is for setting the cache headers (self explanatory.)
Resourcechain and resolver are optional (in this case exactly as the default values.)
@Configuration
public class CustomWebMVCConfig implements WebMvcConfigurer {
private static final int SEVEN_DAYS_IN_SECONDS = 604800;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
registry.setOrder(1).addResourceHandler("/index.html")
.addResourceLocations("classpath:frontend/dist/")
.setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS)
.mustRevalidate())
.setCacheControl(CacheControl.noCache())
.setCacheControl(CacheControl.noStore())
.resourceChain(true)
.addResolver(new PathResourceResolver());
registry.setOrder(0).addResourceHandler("/**")
.addResourceLocations("classpath:frontend/dist/")
.setCachePeriod(SEVEN_DAYS_IN_SECONDS)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
2. At controller level
(yourwebsite.com/index.html)
@GetMapping("/index.html")
public void getIndex(HttpServletResponse response) {
response.setHeader(HttpHeaders.CACHE_CONTROL,
"no-cache, no-store, max-age=0, must-revalidate");
}