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

Posted

技术标签:

【中文标题】如何在 Spring Boot 中启用浏览器缓存【英文标题】:How to enable browser caching in spring boot 【发布时间】:2016-12-31 09:18:54 【问题描述】:

我正在尝试让 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

我已经看到了这个答案How to enable HTTP response caching in Spring Boot,但这似乎不适用于我,因为我没有使用 spring-security,它不在类路径上。

我正在使用带有百里香叶的 spring-boot 1.4.0。

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

【问题讨论】:

即使链接的答案确实谈到了 Spring Security,您是否尝试过任何答案?例如,接受答案的最后一个代码片段与 Spring Security 无关。 "the header "Cache-Control: no-cache"" 你的例子说的是“no-store”,这不一样。而“无商店”与安全相关。 @g00glen00b 只有外部库的答案似乎是合格的,但我认为这应该可以在 spring-boot 本身内解决.. @zeroflagL 感谢您指出错字。它应该是无商店的。 @Wouter 接受答案的最后一个代码块是关于配置WebMvcConfigurerAdapter,与Spring Security 无关。 【参考方案1】:

事实证明它很容易解决。

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

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

【讨论】:

【参考方案2】:

至少在最近(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.

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

【讨论】:

以上是关于如何在 Spring Boot 中启用浏览器缓存的主要内容,如果未能解决你的问题,请参考以下文章

使用带有spring数据缓存的redis时如何启用分布式/集群缓存

在 Spring Boot 中启用 Http 缓存

如何在spring boot中设置禁用浏览器缓存?

如何在使用 Angular 7 和 Spring Boot 构建的 Web 应用程序中使浏览器缓存无效

无法在 Spring Boot Data Rest 中启用 CORS

Spring Boot启用Swagger2