如何在Spring Boot中启用HTTP响应缓存 [英] How to enable HTTP response caching in Spring Boot
问题描述
我使用Spring Boot 1.0.2实现了一个REST服务器。我无法阻止Spring设置禁用HTTP缓存的HTTP标头。
I have implemented a REST server using Spring Boot 1.0.2. I'm having trouble preventing Spring from setting HTTP headers that disable HTTP caching.
我的控制器如下:
@Controller
public class MyRestController {
@RequestMapping(value = "/someUrl", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> myMethod(
HttpServletResponse httpResponse) throws SQLException {
return new ResponseEntity<String>("{}", HttpStatus.OK);
}
}
所有HTTP响应都包含以下标题:
All HTTP responses contain the following headers:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
我已尝试以下方法删除或更改这些标题:
I've tried the following to remove or change those headers:
- 在控制器中调用
setCacheSeconds(-1)
。 - 在控制器中调用
httpResponse.setHeader(Cache-Control,max-age = 123)
。 - 定义
@Bean
,返回WebContentInterceptor
我称之为setCacheSeconds( - 1)
。 - 将属性
spring.resources.cache-period
设置为-1或正值在application.properties
。
- Call
setCacheSeconds(-1)
in the controller. - Call
httpResponse.setHeader("Cache-Control", "max-age=123")
in the controller. - Define
@Bean
that returnsWebContentInterceptor
for which I've calledsetCacheSeconds(-1)
. - Set property
spring.resources.cache-period
to -1 or a positive value inapplication.properties
.
以上都没有任何效果。如何在Spring Boot中为所有或单个请求禁用或更改这些标头?
None of the above have had any effect. How do I disable or change these headers for all or individual requests in Spring Boot?
推荐答案
关闭无缓存HTTP标头由Spring Security设置。这在 http://docs.spring中讨论。 io / spring-security / site / docs / current / reference / htmlsingle /#headers 。
Turns out the no-cache HTTP headers are set by Spring Security. This is discussed in http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#headers.
以下禁用HTTP响应头 Pragma:no-cache
,但不能解决问题:
The following disables the HTTP response header Pragma: no-cache
, but doesn't otherwise solve the problem:
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Prevent the HTTP response header of "Pragma: no-cache".
http.headers().cacheControl().disable();
}
}
我最终完全禁用了Spring Security for public static resources如下(与上面相同的类):
I ended up disabling Spring Security completely for public static resources as following (in the same class as above):
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/static/public/**");
}
这需要配置两个资源处理程序才能正确获取缓存控制头:
This requires configuring two resource handlers to get cache control headers right:
@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter
implements EmbeddedServletContainerCustomizer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Resources without Spring Security. No cache control response headers.
registry.addResourceHandler("/static/public/**")
.addResourceLocations("classpath:/static/public/");
// Resources controlled by Spring Security, which
// adds "Cache-Control: must-revalidate".
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(3600*24);
}
}
另请参阅在Spring Boot& ;;中提供静态Web资源。 Spring Security应用程序。
这篇关于如何在Spring Boot中启用HTTP响应缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!