Spring 多个Web安全配置适配器和筛选器链
我正在尝试为我的应用程序配置几种身份验证类型,为每种身份验证类型使用单独的WebSecurityConfigureAdapter 总体思路是使用WebSecurityConfigureAdapter配置(HttpSecurity http)方法匹配url模式,并使用专用的专有过滤器(包括授权)执行所有身份验证 在初始化过程中,我可以看到每个WebSecurityConfigureAdapter都获得了一个不同的HttpSecurity实例进行配置(假定它有自己的筛选器链),但是在运行时,无论我调用哪个端点,被调用的筛选器链始终是为第一个WebSecurityConfigureAdapter创建的筛选器链 根据文档,Spring应该使用HttpSecurity实例来找到正确的过滤器链进行过滤(根据url模式) 你知道我做错了什么吗?Spring 多个Web安全配置适配器和筛选器链,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我正在尝试为我的应用程序配置几种身份验证类型,为每种身份验证类型使用单独的WebSecurityConfigureAdapter 总体思路是使用WebSecurityConfigureAdapter配置(HttpSecurity http)方法匹配url模式,并使用专用的专有过滤器(包括授权)执行所有身份验证 在初始化过程中,我可以看到每个WebSecurityConfigureAdapter都获得了一个不同的HttpSecurity实例进行配置(假定它有自己的筛选器链),但是在运行时,无论我调
(我正在使用Spring1.5.6-RELEASE来测试这一点)您不需要多个
Web安全配置适配器;只需配置过滤器,使其仅用于匹配URL模式。换句话说,添加所有过滤器,但使过滤器有条件地工作。这就是我最后所做的(使用Spring使用的相同匹配组件路由到我在Spring注册的过滤器中的相应过滤器)。@EranBergman您有没有关于如何实现这一点的代码?萨卡,你能给我一个密码吗?
@Configuration
@EnableWebSecurity
public class DemoMultipleWebSecurityConfigurerAdapter {
@Order(1)
@Configuration
public static class BasicSecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
String endpointPattern = "/api/basic/**";
http.requestMatchers().antMatchers(endpointPattern);
http.csrf().ignoringAntMatchers(endpointPattern);
http.authorizeRequests().antMatchers(endpointPattern).authenticated();
http.addFilterBefore(new MyBasicAuthFilter(), LogoutFilter.class);
}
}
@Order(2)
@Configuration
public static class SSOSecurityAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
String endpointPattern = "/api/sso/**";
http.requestMatchers().antMatchers(endpointPattern);
http.csrf().ignoringAntMatchers(endpointPattern);
http.authorizeRequests().antMatchers(endpointPattern).authenticated();
http.addFilterBefore(new MySSOAuthFilter(), LogoutFilter.class);
}
}
}