How to set up cache-control on a 304 reply with Spring-security

242 Views Asked by At

While using spring-security default CacheControl for all my dynamic end-points, I am trying to make browsers cache my static resources... thus i override the ResourceHandler for them as follow:

@Configuration
@EnableWebMvc
public class WebConfig  extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/")
                .setCacheControl(CacheControl.maxAge(10, TimeUnit.HOURS).cachePrivate());
    }
}

This works fine and when the browser fetches the resource for the first time i get a header with

Cache-Control: max-age=36000

However, when the browser checks if the resource has been updated before the end of cache period using a If-Modified-Since header, the server replies with a 304 response and a header with

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache

And then the browser will once again download the resource when the page is reloaded.

What i want is setting up the Cache-Control header of the 304 response so the browser does not download the resource on every two GET requests. Any pointers using Java annotation? Thanks

0

There are 0 best solutions below