【发布时间】:2021-10-13 07:21:34
【问题描述】:
我正在尝试设置一个简单的 Spring Boot 应用程序。不用@EnableWebFluxSecurity注解就可以启动了。
@Configuration
@EnableWebFluxSecurity
open class ArticleWebSecurityConfig {
@Bean
open fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain = http
.csrf().disable()
.authorizeExchange()
.anyExchange()
.permitAll()
.and()
.build()
}
启动时出现以下错误:
ConfigServletWebServerApplicationContext : 遇到异常 在上下文初始化期间 - 取消刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为“defaultSecurityFilterChain”的 bean 时出错 类路径资源 [org/springframework/boot/autoconfigure/security/servlet/SpringBootWebSecurityConfiguration.class]: 通过方法表达的不满足的依赖关系 'defaultSecurityFilterChain' 参数 0;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 'org.springframework.security.config.annotation.web.builders.HttpSecurity' 可用:预计至少有 1 个符合 autowire 条件的 bean 候选人。依赖注释:{}
说明
org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration 中方法 defaultSecurityFilterChain 的参数 0 需要一个找不到的 'org.springframework.security.config.annotation.web.builders.HttpSecurity' 类型的 bean。
行动:
考虑在你的配置中定义一个 'org.springframework.security.config.annotation.web.builders.HttpSecurity' 类型的 bean。
进程以退出代码 1 结束
我使用了以下@Bean,但仍然得到相同的错误。
@Throws(Exception::class)
@Bean
open fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic()
}
这些是我的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
【问题讨论】:
-
“配置”bean 需要一个自动装配的 HttpSecurity bean 作为其参数。你需要定义一个 HttpSecurity bean,你可以在同一个类中进行。
-
@TomElias 你能回答这个问题吗?我没明白你的真正意思。
-
能否提供一个可重现的小例子
标签: java spring spring-boot spring-security spring-webflux