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

Viewed 53

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.
1 Answers

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.

Related