Spring security 使用spring security在响应中添加安全标头

Spring security 使用spring security在响应中添加安全标头,spring-security,owasp,Spring Security,Owasp,我使用的是spring安全版本3.2。在已验证请求的响应标头中添加诸如X-Frame-options、X-content-type-options等标头 <sec:http auto-config="false"> <sec:headers> <sec:frame-options policy="DENY" /> <sec:content-type-options /> <

我使用的是spring安全版本3.2。在已验证请求的响应标头中添加诸如X-Frame-options、X-content-type-options等标头

<sec:http auto-config="false">
     <sec:headers>
          <sec:frame-options policy="DENY" />
          <sec:content-type-options  />
          <sec:xss-protection enabled="true" block="true" />
     </sec:headers>
</sec:http>

但是,在安全无请求中没有添加这些头

<sec:http security="none" pattern="/spring/loginpage" />


原因可能是什么?

因为如果该模式没有安全性,那么Spring安全性就不会被激活

做你自己的,像这样:

public class SecurityHeadersInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        response.setHeader("Strict-Transport-Security","max-age=31536000 ; includeSubDomains");
        response.setHeader("X-Content-Type-Options", "nosniff");
        response.setHeader("X-Frame-Options", "DENY");
        response.setHeader("X-XSS-Protection", "1; mode=block");
        response.setHeader("Content-Security-Policy", "default-src 'self'");

        super.postHandle(request, response, handler, modelAndView);
    }
}
mvc dispatcher servlet.xml中添加:

<mvc:interceptor>
  <mvc:mapping path="/**"/>
  <bean class="com.example.interceptor.SecurityHeadersInterceptor"/>
</mvc:interceptor>


您应该设置
Cache Control:no store,也必须对任何私有响应(包括如果包含CSRF令牌,如登录表单)重新验证

在这种情况下,是否需要删除xml中添加的头?@san Yes也可以