【发布时间】:2014-08-01 13:47:59
【问题描述】:
我已经使用 Spring Boot 1.0.2 实现了一个 REST 服务器。我无法阻止 Spring 设置禁用 HTTP 缓存的 HTTP 标头。
我的控制器如下:
@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 响应都包含以下标头:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
我尝试了以下方法来删除或更改这些标题:
- 在控制器中调用
setCacheSeconds(-1)。 - 在控制器中调用
httpResponse.setHeader("Cache-Control", "max-age=123")。 - 定义返回
WebContentInterceptor的@Bean,为此我调用了setCacheSeconds(-1)。 - 将属性
spring.resources.cache-period设置为-1 或application.properties中的正值。
以上都没有任何效果。如何在 Spring Boot 中为所有或单个请求禁用或更改这些标头?
【问题讨论】:
-
我认为 Spring Boot 不会这样做(无论如何我尝试过的任何示例都没有)。也许您可以分享一个在响应中包含这些标头的最小项目?
-
你是对的。罪魁祸首原来是 Spring Security。
标签: java spring spring-mvc spring-security spring-boot