java - 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
注意:
- 将第一个配置器的所有匹配器移动到第二个配置器时,事情按预期工作。
- 在启动期间会扫描两个配置器。
知道为什么只考虑第一个配置器FilterSecurityInterceptor
吗?
解决方案
我认为您错过了对requestMatchers
in的调用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) 的调用。
推荐阅读
- amazon-web-services - AWS EC2 更改公共 DNS 主机名,以便它与反向 DNS 查找一起使用
- javascript - Javascript如何将新元素从初始数组推送到相同索引的数组数组中的每个数组?
- python - 如何根据具有连续属性值对对象(元组)进行分组?
- excel - 将宏按钮复制粘贴到不同的工作表时宏不起作用
- django - 如何暂时禁用 Django 索引(用于 SQLite)
- python - 仅在需要时链接方法
- postgresql - Postgres,如何迭代访问数据库中的所有表,子查询?
- docker - Kubernetes 描述 pod - 来自服务器的错误 (NotFound)
- android - SectionedRecyclerViewAdapter 动画
- amazon-web-services - 如何将代理添加到 s3,以便我可以使用托管在 EC2 实例中的服务?