如何在Spring Boot中启用浏览器缓存 [英] How to enable browser caching in spring boot
问题描述
我正试图让Spring Boot让浏览器缓存静态资源。
我的资源位于静态下的类路径中。当我查看发送回的标头时,我看到修改标头设置得很好,但是不知何故,还添加了标头 Cache-Control:no-store。
I'm trying to get spring boot let the browser cache static resources. 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.
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,它不在类路径上。
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 1.4.0。
I am using spring-boot 1.4.0 with thymeleaf.
所以,如何让spring boot不包括Cache-Control标头?
So, how do I let spring boot not include the Cache-Control header?
推荐答案
原来,它很容易解决。
Turns out it is fairly easy to resolve.
目录结构为classpath:/ static / assets。要在响应中不添加缓存控制标头,请添加此类:
The directory structure is 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());
}
}
这仍然让我感到困惑,没有商店是默认为spring-boot。
It still baffled me that "no-store" is the default with spring-boot..
这篇关于如何在Spring Boot中启用浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!