简体   繁体   English

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

[英]How to enable browser caching in spring boot

I'm trying to get spring boot let the browser cache static resources. 我正试图让Spring启动让浏览器缓存静态资源。 My resources are located in the classpath under "static". 我的资源位于“静态”下的类路径中。 When I look at the headers sent back, I see the modification headers being set fine, but somehow the header "Cache-Control: no-store" is also added. 当我查看发回的标头时,我看到修改标头设置正常,但不知何故,标题“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

I have already seen this answer How to enable HTTP response caching in Spring Boot , but this doesn't seem to apply to me as I am not using spring-security, it is not on the classpath. 我已经看到了这个答案如何在Spring Boot中启用HTTP响应缓存 ,但这似乎并不适用于我,因为我没有使用spring-security,它不在类路径上。

I am using spring-boot 1.4.0 with thymeleaf. 我正在使用带有百里香的sp​​ring-boot 1.4.0。

So, how do I let spring boot not include the Cache-Control header? 那么,如何让spring boot不包含Cache-Control头?

Turns out it is fairly easy to resolve. 事实证明它很容易解决。

The directory structure is classpath:/static/assets. 目录结构是classpath:/ static / assets。 To have no cache-control header added to the responds, add this class: 要没有向响应中添加缓存控制头,请添加以下类:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

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

It still baffled me that "no-store" is the default with spring-boot.. 它仍然让我感到困惑的是,“no-store”是spring-boot的默认设置。

At least in recent (2018) versions of SpringBoot, there are properties you can set: 至少在最近的(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 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM