简体   繁体   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

I'm trying to set up basic authentication with Spring Boot and I keep getting this error on startup.我正在尝试使用 Spring Boot 设置基本身份验证,但我在启动时一直收到此错误。 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.我从 Spring 的文档中复制了我的代码,只做了一些小的调整。 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.我对 Spring 还是很陌生,这对我来说就像是巫术,所以您能提供任何见解,我们将不胜感激。

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.

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.在我将 spring-boot-starter-parent 从 2.2.5 升级到 2.7.7 后,问题得到解决。 Also, I added an annotation of EnableWebSecurity to the security另外,我在安全性中添加了 EnableWebSecurity 注释

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

You should post your class "com.atscale.service.BasicAuthSecurityConfiguration" for more information.您应该发布您的 class “com.atscale.service.BasicAuthSecurityConfiguration”以获取更多信息。

Also, check if you already import spring security jar in your pom or gradle file.另外,检查您是否已经在 pom 或 gradle 文件中导入了 spring 安全 jar 文件。

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>

暂无
暂无

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

相关问题 找不到类型为[org.springframework.security.config.annotation.web.builders.HttpSecurity]的限定bean - No qualifying bean of type [org.springframework.security.config.annotation.web.builders.HttpSecurity] found for dependency Spring 引导:考虑在您的配置中定义类型为“org.springframework.security.config.annotation.web.builders.HttpSecurity”的 bean - Spring Boot : Consider defining a bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' in your configuration spring 安全性:NoSuchBeanDefinitionException:找不到 [org.springframework.security.config.annotation.ObjectPostProcessor] 类型的合格 bean - spring security: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found 没有 &#39;org.springframework.security.config.annotation.ObjectPostProcessor 类型的合格 bean<?> &#39; 当我使用 ContextHierarchy 时可用 - No qualifying bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor<?>' available when I use ContextHierarchy NoSuchBeanDefinitionException:没有可用的“org.springframework.boot.web.reactive.error.ErrorAttributes”类型的合格bean: - NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为&#39;com.example.simple.RealApplication&#39;的合格bean - org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.simple.RealApplication' available NoSuchBeanDefinitionException:没有“org.springframework.mail.MailSender”类型的合格bean - NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.MailSender' 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合条件的 bean 类型 - Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为[org.springframework.data.hadoop.hive.HiveOperations]的合格bean - org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.hadoop.hive.HiveOperations] 错误没有可用的“org.springframework.security.oauth2.jwt.JwtDecoder”类型的合格bean - ERROR No qualifying bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM