I'm new to SpringBoot.
I make my project based on a sample project of Springboot. I'd like to control the http headers for cache just for js/css files.
I've added a js file test.js
under src/resources/static
then reference it in greeting.html
.
Then I follow some answers on StackOverflow and add the WebMvcConfiguration.java
and WebSecurityConfig.java
as such but it doesn't work for me.
I'm sure whether WebMvcConfiguration and WebSecurityConfig should not be used together or I've configured something wrong. I prefer a solution with Java Config, not XML.
I open ChromeDevTool to check the response from server. The reponse http header is
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:
Thanks!
UPDATE:
When I removed WebSecurityConfig
, I got the following header
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();
}
}