【问题标题】:Error when using @EnableWebFluxSecurity in spring-boot在 spring-boot 中使用 @EnableWebFluxSecurity 时出错
【发布时间】: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


【解决方案1】:

你需要根据this answer添加@EnableWebFluxSecurity@Configuration

【讨论】:

  • 不,它不起作用。我刚刚更新了我的问题。
猜你喜欢
  • 2020-10-14
  • 1970-01-01
  • 2015-09-28
  • 2019-08-05
  • 2020-09-27
  • 2015-07-27
  • 2015-08-22
  • 2023-04-05
  • 2022-11-01
相关资源
最近更新 更多