Spring security - 使用特定的匹配器创建两个过滤器链

4
我正在将ADFS支持添加到一个现有的Spring项目中。由于我们已经拥有自己的JWT身份验证,希望能够与ADFS身份验证并行工作,因此我想实现一个新的过滤器链,仅处理特定的API请求路径。我的意思是,我想创建:
  • ADFS过滤器链,将处理所有/adfs/saml/** API调用
  • 保留默认的过滤器链,将处理所有其他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();
    }

}


但是在调试时,我发现这个新的过滤器链被设置为匹配“任何”请求。所以我可能设置了错误的匹配器。
1个回答

2
实际上,在阅读官方文档后,答案很简单:(请参见“创建和自定义过滤器链”部分)
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        samlizedConfig(http)
                .antMatcher("/adfs/**");
    }

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接