spring - 未找到 ServerHttpSecurity bean
问题描述
我有一个安全配置类,其中有一个 SecurityWebFilterChain bean。这个 bean 需要一个 ServerHttpSecuirty 实例,但是 spring 说它找不到任何该类型的 bean,尽管在外部库 (org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfiguration) 中创建了一个。我在 github 页面上看到了这个问题,他们说尝试不同的版本,但我使用的是 spring boot 2.4.5,所以它应该可以工作。
我的安全配置类:
@Configuration
public class SecurityConfig {
@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http,
JwtTokenProvider tokenProvider,
ReactiveAuthenticationManager reactiveAuthenticationManager) {
final String TAG_SERVICES = "/api/**";
return http.csrf(ServerHttpSecurity.CsrfSpec::disable)
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
.authenticationManager(reactiveAuthenticationManager)
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
.authorizeExchange(it -> it
.pathMatchers(HttpMethod.POST, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.PUT, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.GET, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(HttpMethod.DELETE, TAG_SERVICES).hasAnyRole("USER","ADMIN")
.pathMatchers(TAG_SERVICES).authenticated()
.anyExchange().permitAll()
)
.addFilterAt(new JwtTokenAuthenticationFilter(tokenProvider), SecurityWebFiltersOrder.HTTP_BASIC)
.build();
}
}
我的应用类
@ConfigurationPropertiesScan
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) 公共类 TestPlatformBackendApplication {
public static void main(String[] args) {
SpringApplication.run(TestPlatformBackendApplication.class, args);
}
}
外部库 Bean:
@Bean({"org.springframework.security.config.annotation.web.reactive.HttpSecurityConfiguration.httpSecurity"})
@Scope("prototype")
ServerHttpSecurity httpSecurity() {
ServerHttpSecurityConfiguration.ContextAwareServerHttpSecurity http = new ServerHttpSecurityConfiguration.ContextAwareServerHttpSecurity();
return http.authenticationManager(this.authenticationManager()).headers().and().logout().and();
}
解决方案
正如Toerktumlare在评论 ( 1 , 2 ) 中推荐的那样,我添加@EnableWebFluxSecurity
到我的安全配置中:
@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
但我还在@SpringBootApplication
注释中的排除项中添加了以下内容。
@ConfigurationPropertiesScan
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
public class TestPlatformBackendApplication {
public static void main(String[] args) {
SpringApplication.run(TestPlatformBackendApplication.class, args);
}
}
推荐阅读
- arrays - PowerShell从数组中过滤基于以分号分隔的电子邮件,以管道分隔的数组中的对象值
- java - 如何在同一流语句中对 Collectors.groupedBy 的结果进行排序?
- sql - 如何聚合行并转换为列?
- django - 没有主键和 id 的 django 模型
- c# - 如何在.net核心中通过kafka和web api之间的标准开放遥测协议进行消息跟踪
- npm-install - 已安装 NPM Sim 模块和 Sim Cordova 插件,但仍未检测到模块
- ios - Xcode 构建错误“架构 arm64 的未定义符号”
- r - 如何在 R 中使用 rvest 在 URL 上抓取多个表
- reactjs - 一个组件正在更改要控制的电子邮件类型的不受控制的输入
- c# - 是否可以从具有扩展名的项目中创建一个对象