如何在SpringBoot中将缓存控制设置为CSS和JS文件?

Kan*_* Lu 5 cache-control spring-security spring-boot

我是SpringBoot的新手。

我基于Springboot示例项目来创建我的项目。我想仅针对js / css文件控制用于缓存的http标头。

我在下面添加了一个js文件test.jssrc/resources/static然后在中引用它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:
Run Code Online (Sandbox Code Playgroud)

谢谢!

更新:当我删除时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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

WebMvcConfiguration.java

@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

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

WebSecurityConfig.java

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

ETL*_*ETL 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.
Run Code Online (Sandbox Code Playgroud)

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