如何在spring boot中启用浏览器缓存

Wou*_*ter 5 java spring caching spring-mvc spring-boot

我正试图让Spring启动让浏览器缓存静态资源.我的资源位于"静态"下的类路径中.当我查看发回的标头时,我看到修改标头设置正常,但不知何故,标题"Cache-Control:no-store"也被添加.

HTTP/1.1 200
Last-Modified: Wed, 24 Aug 2016 08:50:16 GMT
Cache-Control: no-store
Accept-Ranges: bytes
Content-Type: text/css
Content-Length: 434554
Date: Wed, 24 Aug 2016 09:42:42 GMT
Run Code Online (Sandbox Code Playgroud)

我已经看到了这个答案如何在Spring Boot中启用HTTP响应缓存,但这似乎并不适用于我,因为我没有使用spring-security,它不在类路径上.

我正在使用带有百里香的sp​​ring-boot 1.4.0.

那么,如何让spring boot不包含Cache-Control头?

Wou*_*ter 6

事实证明它很容易解决。

目录结构为 classpath:/static/assets。要在响应中不添加缓存控制标头,请添加此类:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/static/assets/").setCacheControl(CacheControl.empty());
    }
}
Run Code Online (Sandbox Code Playgroud)

仍然让我感到困惑的是,spring-boot 的默认设置是“no-store”。


ETL*_*ETL 5

至少在最近(2018)版本的 SpringBoot 中,您可以设置以下属性:

    spring.resources.cache.cachecontrol.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
    spring.resources.cache.cachecontrol.cache-public= # Indicate that any cache may store the response.
    spring.resources.cache.cachecontrol.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
    spring.resources.cache.cachecontrol.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
    spring.resources.cache.cachecontrol.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
    spring.resources.cache.cachecontrol.no-store= # Indicate to not cache the response in any case.
    spring.resources.cache.cachecontrol.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
    spring.resources.cache.cachecontrol.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
    spring.resources.cache.cachecontrol.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
    spring.resources.cache.cachecontrol.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
    spring.resources.cache.cachecontrol.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.
Run Code Online (Sandbox Code Playgroud)

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html