【发布时间】:2017-06-19 06:33:39
【问题描述】:
我是 SpringBoot 新手。
我根据sample project of Springboot 制作我的项目。我想为 js/css 文件控制缓存的 http 标头。
我在src/resources/static 下添加了一个js 文件test.js,然后在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();
}
}
【问题讨论】:
-
@dur,我想设置 31556926。我不知道在 Spring Security 中设置了什么没有缓存。我怎样才能实现它?
-
@dur,我已经删除了 WebSecurityConfig 和 WebMvcConfiguration。我在
test.js的响应http标头中得到了no store -
不,没有其他过滤器。 .css 也是如此
标签: spring-boot spring-security cache-control