【问题标题】:How do I set cache control to css and js files in SpringBoot?如何在 Spring Boot 中为 css 和 js 文件设置缓存控制?
【发布时间】:2017-06-19 06:33:39
【问题描述】:

我是 SpringBoot 新手。

我根据sample project of Springboot 制作我的项目。我想为 js/css 文件控制缓存的 http 标头。

我在src/resources/static 下添加了一个js 文件test.js,然后在greeting.html 中引用它。

然后我按照 StackOverflow 上的一些答案添加 WebMvcConfiguration.javaWebSecurityConfig.java,但它对我不起作用。

我确定 WebMvcConfiguration 和 WebSecurityConfig 是否不应该一起使用,或者我配置了错误。我更喜欢使用 Java Config 的解决方案,而不是 XML。

我打开 ChromeDevTool 来检查服务器的响应。响应 http 标头是

Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 10:53:14 GMT
Expires:
Last-Modified:Thu, 02 Feb 2017 10:52:49 GMT
Pragma:

谢谢!

更新: 当我删除WebSecurityConfig 时,我得到了以下标题

Accept-Ranges:bytes
Cache-Control:no-store
Content-Length:20
Content-Type:application/javascript
Date:Thu, 02 Feb 2017 11:14:51 GMT
Last-Modified:Thu, 02 Feb 2017 11:14:20 GMT

greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script th:src="@{/test.js}"></script>
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

WebMvcConfiguration.java

@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/test.js").addResourceLocations("classpath:/resources/").setCachePeriod(31556926);
    }
}

WebSecurityConfig.java

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable()
              .headers()
              .defaultsDisabled()
              .cacheControl();
  }
}

【问题讨论】:

  • @dur,我想设置 31556926。我不知道在 Spring Security 中设置了什么没有缓存。我怎样才能实现它?
  • @dur,我已经删除了 WebSecurityConfig 和 WebMvcConfiguration。我在test.js的响应http标头中得到了no store
  • 不,没有其他过滤器。 .css 也是如此

标签: spring-boot spring-security cache-control


【解决方案1】:

现代 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

【讨论】:

  • 如果有人遇到这个问题,max-age 需要是一个持续时间字符串,因此 7 天:spring.resources.cache.cachecontrol.max-age: 7d
【解决方案2】:

我也是springboot的新手。我刚刚将以下内容添加到我的 application.properties 文件中。

spring.resources.chain.cache= false

它解决了我的问题,直到我找到更高级的缓存控制。

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 2011-02-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2016-09-21
    • 2013-08-25
    相关资源
    最近更新 更多