我是SpringBoot的新手。
我根据sample project of Springboot创建项目。我想为js / css文件控制缓存的http标头。
我在test.js
下添加了一个js文件src/resources/static
,然后在greeting.html
中引用它。
然后我在StackOverflow上关注一些答案并添加WebMvcConfiguration.java
和WebSecurityConfig.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();
}
}
答案 0 :(得分: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
答案 1 :(得分:0)
我也是 springboot 的新手。我刚刚将以下内容添加到我的 application.properties 文件中。
spring.resources.chain.cache= false
它解决了我的问题,直到我找到更高级的缓存控制。