如何将标头添加到Spring Security忽略的请求中

问题描述 投票:2回答:2

我的Spring Security配置是

@Override
  public void configure(WebSecurity web) throws Exception {
    web
      .ignoring()
         .antMatchers("/resources/**"); // #3
  }

qazxsw poi忽略的文档说

允许添加Spring Security应忽略的RequestMatcher实例。 ...通常,注册的请求应该只是静态资源的请求。

我想为从资源提供的文件添加一些标头。例如:Taken from here.Strict-Transport-Security: max-age=31536000

我怎么能这样做?

spring-mvc spring-security
2个回答
0
投票

将其改为的一种解决方案

X-Content-Type-Options: nosniff

示例如何允许缓存控制标头加上所有默认的弹簧安全标头。


0
投票

我一直在努力解决同样的问题。当我忽略WebSecurity中的特定请求时,标题就消失了。

我通过对添加标头的每个请求应用过滤器来修复丢失的标头。

protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/resources/**").permitAll()
       .and()
       .antMatcher("/resources/**").headers().cacheControl()
}

过滤器代码如下所示。这里需要注意的重要一点是Filter必须声明为@Override protected void configure(HttpSecurity http) throws Exception { http .addFilterBefore(securityHeaderFilter, BasicAuthenticationFilter.class) ... } 。当您错过@Component注释时,过滤器将被忽略。

@Component
© www.soinside.com 2019 - 2024. All rights reserved.