首页 > 解决方案 > Spring Boot中的多个WebSecurityConfigurerAdapter不能一起工作

问题描述

我有一个 Spring Boot 应用程序。它有 2 个WebSecurityConfigurerAdapter,其中一个在 Spring 库依赖项中(这对其他应用程序很常见):

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "com.mycomp.common.security.**")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CommonWebSecurityConfig extends WebSecurityConfigurerAdapter {
  http
            
            .authorizeRequests()
    .antMatchers("/configurations/**").hasAnyAuthority(TechnicalScope.ACTUATOR_ADMIN.getValue(), SystemScope.ACTUATOR_ADMIN.getValue())
    .antMatchers(GET, "/jobs/scheduling/**").hasAuthority(TechnicalScope.JOB_READ.getValue())
    .antMatchers(GET, "/odata.svc/**").hasAuthority(TechnicalScope.ODATA_READ.getValue())
            
}

第二个:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "com.mycomp.accounts.**.security")
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  http
        
        .authorizeRequests()
            
            .mvcMatchers(GET, "/swagger-ui.html").permitAll()
            .mvcMatchers(GET, "/webjars/springfox-swagger-ui/**").permitAll()
            .mvcMatchers(GET, "/swagger-resources/**").permitAll()
            .mvcMatchers(GET, "/v2/api-docs/**").permitAll()

            .mvcMatchers(GET, AccountController.BASE_PATH).hasAuthority(Scope.ACCOUNT_READ.getValue())
            .mvcMatchers(PATCH, AccountController.BASE_PATH).hasAuthority(Scope.ACCOUNT_UPDATE.getValue())
    .and()
                .oauth2ResourceServer()
                .jwt()
                .jwtAuthenticationConverter(getJwtAuthoritiesConverter());
            
}

问题:针对第一个WebSecurityConfigurerAdapter ONLY的匹配器验证请求。第二个匹配器被忽略。尝试调试时,我可以看到仅使用第一个配置器匹配器进行FilterSecurityInterceptor.obtainSecurityMetadataSource维护。requestMap

注意

  1. 将第一个配置器的所有匹配器移动到第二个配置器时,事情按预期工作。
  2. 在启动期间会扫描两个配置器。

知道为什么只考虑第一个配置器FilterSecurityInterceptor吗?

标签: javaspringspring-bootspring-securityspring-security-oauth2

解决方案


我认为您错过了对requestMatchersin的调用CommonWebSecurityConfig

尝试这样做:

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "com.mycomp.common.security.**")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CommonWebSecurityConfig extends WebSecurityConfigurerAdapter {
  http.requestMatchers()
    .antMatchers("/configurations/**").hasAnyAuthority(TechnicalScope.ACTUATOR_ADMIN.getValue(), SystemScope.ACTUATOR_ADMIN.getValue())
    .antMatchers(GET, "/jobs/scheduling/**").hasAuthority(TechnicalScope.JOB_READ.getValue())
    .antMatchers(GET, "/odata.svc/**").hasAuthority(TechnicalScope.ODATA_READ.getValue())
    .authorizeRequests();
}

这里是java文档requestMatchers

允许指定将在哪些 HttpServletRequest 实例上调用此 HttpSecurity。此方法允许为多个不同的 RequestMatcher 实例轻松调用 HttpSecurity。如果只需要一个 RequestMatcher,请考虑使用 mvcMatcher(String)、antMatcher(String)、regexMatcher(String) 或 requestMatcher(RequestMatcher)。调用 requestMatchers() 不会覆盖先前对 mvcMatcher(String)}、requestMatchers()、antMatcher(String)、regexMatcher(String) 和 requestMatcher(RequestMatcher) 的调用。


推荐阅读