我设置过滤器bean时插入了重复的Cache-Control标头

时间:2015-03-04 15:18:25

标签: spring spring-boot spring-cache

我设置了一个过滤器bean来插入和重置Cache-Control标头。这工作正常,除了在过滤器之后的一点,插入额外的Cache-Control标头。

我正在使用Spring Boot。 什么可能导致问题的任何解决方案?

@Component
public class CacheControlFilter implements Filter {

     @Override
     public void init(FilterConfig filterConfig) throws ServletException {}

     @Override
     public void destroy() {}

     @Override
     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        Calendar expires = Calendar.getInstance();
        expires.add(Calendar.HOUR, 24);

        // Intercept response header
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setDateHeader("Expires", expires.getTimeInMillis());
        resp.setHeader("Cache-Control", "max-age=2048");
        chain.doFilter(request, resp);
     }
}

请参阅重复的Cache-Control标题:

HTTP/1.1 200 OK  
...  
Cache-Control: max-age=2048  
Cache-Control: no-cache, no-store, max-age=0, must-revalidate  
Expires: Fri, 26 Sep 2014 18:21:30 GMT  
Expires: 0  
Pragma: no-cache  
Content-Type: image/png  
...  

1 个答案:

答案 0 :(得分:5)

您使用的是Spring-security吗?

Spring安全性也会自动添加它们,您可以在配置中禁用它们,如下所示:

class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override protected void configure(HttpSecurity http) throws Exception {
        //... Rest of config

        http.headers().disable()

详情请见此处:http://docs.spring.io/autorepo/docs/spring-security/3.2.2.RELEASE/apidocs/org/springframework/security/config/annotation/web/configurers/HeadersConfigurer.html

您还可以根据需要配置要打开/关闭的特定标头(请参阅该API文档中的其他方法,例如cacheControl()等)