【问题标题】:NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' availableNoSuchBeanDefinitionException: No qualifying bean of type \'org.springframework.security.config.annotation.web.builders.HttpSecurity\' available
【发布时间】:2022-12-27 01:49:19
【问题描述】:

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.

【问题讨论】:

    标签: java spring spring-boot basic-authentication


    【解决方案1】:

    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.

    【讨论】:

      【解决方案2】:

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 2022-12-12
        • 2016-11-07
        • 2022-10-14
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多