如何在Spring Boot中启用浏览器缓存

7

我正在尝试让Spring Boot允许浏览器缓存静态资源。我的资源位于类路径下的“静态”文件夹中。当我查看返回的头信息时,我发现修改头已经正确设置,但不知何故也添加了“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

我已经看到了这个回答在Spring Boot中如何启用HTTP响应缓存,但是这似乎不适用于我,因为我没有使用spring-security,它不在类路径中。
我正在使用带有thymeleaf的spring-boot 1.4.0。
那么,如何让spring boot不包含Cache-Control头?

即使链接的答案确实涉及Spring Security,但您尝试过任何答案吗?例如,被接受的答案的最后一个代码片段并不涉及Spring Security。 - g00glen00b
“Cache-Control: no-cache” 头部信息。您的示例中使用了“no-store”,这不是同一个意思。“no-store”与安全相关。 - a better oliver
@g00glen00b 只有使用外部库的答案似乎是可行的,但我认为这应该可以在 spring-boot 内部解决。 - Wouter
@zeroflagL,感谢指出拼写错误。应该是no-store。 - Wouter
@Wouter,被接受的答案的最后一个代码块是关于配置WebMvcConfigurerAdapter的,与Spring Security无关。 - g00glen00b
@g00glen00b 调试了一下解决方案,找到了答案。现在发布它。 - Wouter
2个回答

7
结果如下:

结果表明这相当容易解决。

目录结构是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());
    }
}

我仍然感到困惑的是,"no-store"在spring-boot中是默认设置...


5

至少在SpringBoot最近的版本(2018年)中,有一些可以设置的属性:

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.

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接