-1

I am trying to setup a simple spring boot application. It can be started if I don't use @EnableWebFluxSecurity annotatoin.

    @Configuration
    @EnableWebFluxSecurity
    open class ArticleWebSecurityConfig {

        @Bean
        open fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain = http
            .csrf().disable()
            .authorizeExchange()
            .anyExchange()
            .permitAll()
            .and()
            .build()
    }

The following error occurrs when I start it:

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultSecurityFilterChain' defined in class path resource [org/springframework/boot/autoconfigure/security/servlet/SpringBootWebSecurityConfiguration.class]: Unsatisfied dependency expressed through method 'defaultSecurityFilterChain' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Description

Parameter 0 of method defaultSecurityFilterChain in org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration required a bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' in your configuration.

Process finished with exit code 1

I used the following @Bean but stil get the same errror.

    @Throws(Exception::class)
    @Bean
    open fun configure(http: HttpSecurity) {
        http
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic()
    }

These are my dependencies

        <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>

mahan
  • 12,366
  • 5
  • 48
  • 83

3 Answers3

1

I had the same issue.Some of your dependencies are bringing spring-boot-starter-web. You need to find who using mvn dependency:tree and exclude it in your pom.xml.

<exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </exclusion>
 </exclusions>

And also add as seperate dependecy the

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Also another addition you should make is add : spring.main.web-application-type=reactive in application properties

0

You need to add both @EnableWebFluxSecurity and @Configuration according to this answer.

gcpdev
  • 442
  • 6
  • 20
0
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

Adding above dependency solved it for me. Using Spring 6, Boot 3, Java 17.