java - 禁用 Spring Security header 不起作用

标签 java security caching spring-security spring-boot

我需要在我的 Spring Security conf 中禁用缓存控制 header 。

根据文档,一个简单的 http.headers.disable() 应该可以做到,但我仍然看到了

Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Expires:0
Pragma:no-cache

响应中的 header 。

我当前的安全配置是:

http.antMatcher("/myPath/**") // "myPath" is of course not the real path
    .headers().disable()
    .authorizeRequests()
     // ... abbreviated
    .anyRequest().authenticated();

到目前为止我尝试过的事情:

application.properties

我添加了 security.headers.cache=false 行,但这没有任何区别。

使用过滤器

我尝试了以下过滤器:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  chain.doFilter(request, new HttpServletResponseWrapper((HttpServletResponse) response) {
      @Override
      public void setHeader(String name, String value) {
        if (name.equalsIgnoreCase("Cache-Control")) {
          value = "";
        } else if (name.equalsIgnoreCase("Expires")) {
          value = "";
        } else if (name.equalsIgnoreCase("Pragma")) {
          value = "";
        }
        super.setHeader(name, value);
      }
  });
}

添加日志记录后,我看到此过滤器仅写入 X-XSS-Protection header ,所有缓存 header 都稍后写入某处,并且此过滤器无权“覆盖”它们。即使我将此过滤器添加到安全过滤器链的最后位置,也会发生这种情况。

使用拦截器

我尝试了以下拦截器:

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    String requestUri = request.getRequestURI();
    response.setHeader("Cache-Control", "max-age=3600");
    response.setHeader("Expires", "3600");
    response.setHeader("Pragma", "");
}

这(完全可以预见)只是添加了 header ,这意味着除了拦截器添加的 header 之外,原始的 no-cache header 仍然出现。

我已经无计可施了。如何去掉 Spring security 设置的缓存控制 header ?

最佳答案

可能会有帮助:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    // ...
    .headers()
        .defaultsDisabled()
        .cacheControl();
}
}

http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html#headers-cache-control

关于java - 禁用 Spring Security header 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006029/

相关文章:

java - 从 Java 中的另一个线程更新 JTextField

java - Jboss 7.1 中的 com.fasterxml.jackson.databind.JsonMappingException

java - 如何在 libGDX 中测量触摸压力

c# - 将 Salt 作为明文存储在包含密文的文件中

java - Spring 数据休息 : RepositoryEventHandler methods not invoked

php - php中的密码/登录系统

ios - 如何使用 "sequential finger detection"的新 iOS 9.2 Touch ID 功能

java - Hibernate 双向多对多关联创建重复项

c# - HttpContext.Current.Cache 的替代品

java - 关于 JCS 的信息