【发布时间】:2016-01-17 19:19:23
【问题描述】:
如何在 Spring Boot 中为静态资源添加 Cache-Control HTTP 标头?
尝试在应用程序中使用过滤器组件,该组件可以正确写入标头,但 Cache-Control 标头会被覆盖。
@Component
public class CacheBustingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResp = (HttpServletResponse) resp;
httpResp.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
httpResp.setHeader("This-Header-Is-Set", "no-cache, no-store, must-revalidate");
httpResp.setHeader("Expires", "0");
chain.doFilter(req, resp);
}
我在浏览器中得到的是:
Cache-Control:no-store
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
我想要的是:
Cache-Control:no-cache, no-store, must-revalidate
This-Header-Is-Set:no-cache, no-store, must-revalidate
Expires:0
【问题讨论】:
-
也试过了,还是不行。它添加了 X-headers 和各种东西。但 Cache-Control 始终是“无存储”。
标签: java spring-boot cache-control