matthew_mariani
matthew_mariani

Reputation: 83

NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available

I'm trying to set up basic authentication with Spring Boot and I keep getting this error on startup. I've seen several examples with almost the exact same code as I have here and I can't tell what I'm doing wrong. I copied my code from Spring's documentation with only minor tweaks. I'm still very new to Spring and this all seems like witchcraft to me so any insights you can offer would be greatly appreciated.

Code:

package com.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Configuration
public class BasicAuthSecurityConfiguration
{
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .httpBasic();

        return http.build();
    }

    @Bean
    public InMemoryUserDetailsManager userDetailsService() throws IOException {
        String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        String appConfigPath = rootPath + "application.properties";
        Properties appProps = new Properties();
        appProps.load(new FileInputStream(appConfigPath));

        UserDetails user = User
                .withUsername(appProps.getProperty("requestUsername"))
                .password(appProps.getProperty("requestPassword"))
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(user);
    }
}

Error Message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method filterChain in com.atscale.service.BasicAuthSecurityConfiguration 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.

Upvotes: 8

Views: 13150

Answers (6)

Eddy
Eddy

Reputation: 3733

Version:

<spring-cloud.version>2023.0.0</spring-cloud.version>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.2.1</version>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>4.1.0</version>

Code:

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http
            .authorizeExchange()
            .pathMatchers("/order/**").permitAll()
            .pathMatchers("/user/**").hasRole("ADMIN")
            .anyExchange().authenticated()
            .and()
            .httpBasic().and()
            .formLogin();
        return http.build();
    }
}

Upvotes: 1

Ashrik Ahamed
Ashrik Ahamed

Reputation: 455

It looks like you are extending WebSecurityConfigurerAdapter And when override its method, which is Configure(HttpSecurity http)

It should be annotated with @Override not @Bean or anything Else

Upvotes: 0

Kuppusamy
Kuppusamy

Reputation: 163

In my case it was a webflux application but was injecting HttpSecurity so autoconfigure can't find HttpSecurity. When i changed it to ServerHttpSecurity everything worked fine.

Upvotes: 3

Tak Sin Luk
Tak Sin Luk

Reputation: 31

I recently faced the same error. The issue was fixed after I upgraded from 2.2.5 to 2.7.7 for spring-boot-starter-parent. Also, I added an annotation of EnableWebSecurity to the security

Reference: https://www.baeldung.com/spring-boot-security-autoconfiguration

Upvotes: 3

Arnas
Arnas

Reputation: 662

I recently faced the same error. Adding this dependency solved the problem for me.

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

Upvotes: 1

Daniel
Daniel

Reputation: 56

You should post your class "com.atscale.service.BasicAuthSecurityConfiguration" for more information.

Also, check if you already import spring security jar in your pom or gradle file.

Upvotes: 0

Related Questions