3

我设置了一个过滤器 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  
...  
4

1 回答 1

6

你在使用 Spring 安全性吗?

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

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()等)

于 2015-03-04T15:33:26.233 回答