spring-security - 在 Spring Boot 中使用多个 WebSecurityConfigurerAdapter

标签 spring-security spring-boot

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

思路如下:

  • 有一个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);
             }
         }
     }
    
  • 我希望上面是一种任何人都可以通过 @ComponentScan 包含的库类并对自定义身份验证进行排序。显然他们想提供定制的HttpSecurity保护edpoints。尝试类似:

  •  @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();
         }
     }
    

    显然测试 URL 不应该是可访问的,因为我的用户不是角色 NON_EXISTING 的成员.不幸的是,她是。

    如果我移动安全authorizeRequests()配置类表单的一部分 1. 在添加安全过滤器之后,它会按预期阻止访问。但在我的情况下,第二个配置似乎被忽略了。

    我还调试了configure()方法并注意到 HttpSecurity不是有一点气味的同一个物体。

    任何提示我如何使这项工作非常受欢迎。

    目标总结:
  • 有一个WebSecurityConfigurerAdapter添加过滤器并对库的用户隐藏
  • 让用户定义自己的自定义端点安全

  • Spring Boot 1.1.6-RELEASE

    最佳答案

    定义一个特殊的接口(interface)

    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 。也可以有多个实现,只需将它们 Autowiring 为列表并迭代并在主配置中使用它们。

    关于spring-security - 在 Spring Boot 中使用多个 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114269/

    相关文章:

    hibernate - 使用 mapstruct 映射 Hibernate 实体

    grails - 在Grails中显示Spring Security类的属性

    spring - 如何在我的 Controller 中验证(@Valid)之前检查安全访问(@Secured 或 @PreAuthorize)?

    spring-security - 如何根据 RequestHeaderAuthenticationFilter 将身份验证管理器委派给特定的 CustomUserDetailsS​​ervice?

    spring - Spring Boot 应用程序之间的 Jaeger 中未显示服务依赖项

    java - 如何在spring boot中使用@Bean为抽象类创建bean

    angularjs - 忽略所有 HTTP.OPTIONS 调用的身份验证是否安全?

    spring - 在 AbstractAuthenticationProcessingFilter 中连接服务以添加身份验证详细信息

    java - 包 org.springframework.boot 不存在

    java - Spring Boot注册服务器(Eureka服务器)