小编典典

在Spring Boot中使用多个WebSecurityConfigurerAdapter

spring-boot

我有2个扩展的类WebSecurityConfigurerAdapter。并且不能让他们一起工作。

这个想法如下:

  1. 有一个WebSecurityConfigurerAdapter仅将自定义过滤器添加到安全链的过滤器。过滤器会执行一些自定义身份验证,然后保存Authentication到中SecurityContext。这通常可以正常工作。配置如下(省略导入):

    @Order(1)
    @Configuration
    @EnableWebMvcSecurity
    public class BestSecurityConfig extends WebSecurityConfigurerAdapter {

     @Autowired
     private BestPreAuthenticationFilter ssoAuthenticationFilter;
    
     @Bean
     protected FilterRegistrationBean getSSOAuthenticationFilter() {
         FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(ssoAuthenticationFilter);
    
         // Avoid include to the default chain
         filterRegistrationBean.setEnabled(false);
    
         return filterRegistrationBean;
     }
    
     @Override
     protected void configure(HttpSecurity http) throws Exception {
         http
            .addFilterAfter(ssoAuthenticationFilter, SecurityContextPersistenceFilter.class);
    
     }
    
     @Configuration
     protected static class AuthenticationConfiguration extends
             GlobalAuthenticationConfigurerAdapter {
    
         @Autowired
         private BestAuthenticationProvider authenticationProvider;
    
         @Override
         public void configure(AuthenticationManagerBuilder auth) throws Exception {
             auth.authenticationProvider(authenticationProvider);
         }
     }
    

    }

  2. 我希望以上内容是任何人都可以通过@ComponentScan并获得自定义身份验证排序的库类。显然,他们希望提供定制HttpSecurity的安全点。尝试类似的东西:

    @Configuration
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    public class SecurityConfig extends WebSecurityConfigurerAdapter {

     @Override
     protected void configure(HttpSecurity http) throws Exception {
         http
             .csrf().disable()
             .authorizeRequests()
             .antMatchers("/testUrl").hasRole("NON_EXISTING")
             .anyRequest().authenticated();
     }
    

    }

显然,由于我的用户不是role成员,因此不应访问测试URL NON_EXISTING。不幸的是她。

如果我将安全性authorizeRequests()部分移到配置类形式1.,然后添加安全性过滤器,则它将按预期阻止访问。但就我而言,第二种配置似乎被忽略了。

我还调试了这些configure()方法,并发现那HttpSecurity不是一个闻起来有点不同的对象。

任何提示如何使这项工作倍受赞赏。

总结目标:

  • 有一个WebSecurityConfigurerAdapter添加过滤器并且对 用户隐藏的过滤器 __
  • 让用户定义自己的自定义端点安全性

Spring Boot 1.1.6-发布


阅读 3223

收藏
2020-05-30

共1个答案

小编典典

定义一个特殊的接口

public interface ServiceWebSecurityConfigurer {
    void configure(HttpSecurity http) throws Exception;
}

然后只有一个ConfigurerAdapter:

public class MyConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired(required = false)
    ServiceWebSecurityConfigurer serviceSecConfig;

    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests(). // whatever

        if (serviceSecConfig != null) serviceSecConfig.configure(http);

        http.authorizeRequests(). // whatever
    }
}

然后在需要时在其他地方实现ServiceWebSecurityConfigurer。也可以有多种实现,只需将它们自动连接为列表,然后迭代并在主配置中使用它们即可。

2020-05-30