繁体   English   中英

NoSuchBeanDefinitionException:没有可用的“org.springframework.security.config.annotation.web.builders.HttpSecurity”类型的合格 bean

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

我正在尝试使用 Spring Boot 设置基本身份验证,但我在启动时一直收到此错误。 我已经看到几个示例,它们的代码几乎与我在这里看到的完全相同,但我不知道我做错了什么。 我从 Spring 的文档中复制了我的代码,只做了一些小的调整。 我对 Spring 还是很陌生,这对我来说就像是巫术,所以您能提供任何见解,我们将不胜感激。

代码:

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);
    }
}

错误信息:

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

我最近遇到了同样的错误。 在我将 spring-boot-starter-parent 从 2.2.5 升级到 2.7.7 后,问题得到解决。 另外,我在安全性中添加了 EnableWebSecurity 注释

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

您应该发布您的 class “com.atscale.service.BasicAuthSecurityConfiguration”以获取更多信息。

另外,检查您是否已经在 pom 或 gradle 文件中导入了 spring 安全 jar 文件。

我最近遇到了同样的错误。 添加此依赖项为我解决了问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM