【问题标题】:Spring security - create 2 filter chains with specific matchersSpring security - 创建 2 个具有特定匹配器的过滤器链
【发布时间】:2021-05-28 16:35:27
【问题描述】:

我正在为现有的 Spring 项目实施 ADFS 支持。 由于我们已经有了自己的 JWT 身份验证,我们希望与 ADFS 身份验证并行工作,因此我想实现一个新的过滤器链,它只处理某些 API 请求路径。 我的意思是我想创建:

  • 将处理所有 /adfs/saml/** API 调用的 ADFS 过滤器链
  • 保留将处理所有其余 API 调用的默认过滤器链

我正在使用ADFS spring security lib 来定义过滤器链,如下所示:

public abstract class SAMLWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

//some code

 protected final HttpSecurity samlizedConfig(final HttpSecurity http) throws Exception {
        http.httpBasic().authenticationEntryPoint(samlEntryPoint())
                .and()
                .csrf().ignoringAntMatchers("/saml/**")
                .and()
                .authorizeRequests().antMatchers("/saml/**").permitAll()
                .and()
                .addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class)
                .addFilterAfter(filterChainProxy(), BasicAuthenticationFilter.class);

        // store CSRF token in cookie
        if (samlConfigBean().getStoreCsrfTokenInCookie()) {
            http.csrf()
                    .csrfTokenRepository(csrfTokenRepository())
                    .and()
                    .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class);
        }

        return http;
    }
}

我扩展了这个类:

@EnableWebSecurity
@Configuration
@Order(15)
@RequiredArgsConstructor
public class ADFSSecurityConfiguration extends SAMLWebSecurityConfigurerAdapter {
   @Override
    protected void configure(final HttpSecurity http) throws Exception {
        samlizedConfig(http)
                .authorizeRequests()
                .antMatchers("/adfs")
                .authenticated();
    }

}

但是在调试时我看到这个新的过滤器链被设置为匹配“任何”请求。 所以我可能把匹配器设置错了。

【问题讨论】:

    标签: spring spring-security adfs matcher spring-security-rest


    【解决方案1】:

    其实看了official docs 的答案很简单: (请参阅“创建和自定义过滤器链”部分)

        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            samlizedConfig(http)
                    .antMatcher("/adfs/**");
        }
    

    它不应该放在.authorizeRequests() 之后,而是放在第一个匹配器上。

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2015-07-10
      • 2019-04-13
      • 2014-10-16
      • 2013-09-30
      相关资源
      最近更新 更多