创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败
Posted
技术标签:
【中文标题】创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败【英文标题】:Error creating bean with name 'securityConfig': Injection of autowired dependencies failed 【发布时间】:2014-06-09 08:11:05 【问题描述】:我正在尝试结合 Java-config 和 xml-config 进行 Spring 安全认证。但我收到一个错误:
我的代码似乎有什么问题?一直在谷歌搜索答案,但没有找到任何答案。
提前致谢。希望你能帮助我。
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setAuthenticationConfiguration(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
java-config: SecurityConfig.java
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception
http
.authorizeRequests()
.antMatchers("/webapp/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
@Autowired
public void registerGlobalAuthentication(
AuthenticationManagerBuilder auth) throws Exception
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
web.xml
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我已经在我的 servlet-context.xml 中声明了组件扫描
<context:component-scan base-package="ph.project.p3.conf" />
【问题讨论】:
你是如何解决这个问题的? 【参考方案1】:您可以尝试添加@Component
注释。这样自动装配应该可以工作。
【讨论】:
【参考方案2】:您需要在 Spring-config 文件中添加<context:component-scan>
。否则,您的应用程序将不会扫描您的包结构以在应用程序上下文中查找和注册 bean。
语法: <context:component-scan base-package="org.example.<yourapplicationName>"/>
例如:<context:component-scan base-package="oph.project.p3.conf"/>
【讨论】:
嗨@AnilSatija,感谢您的回复。正如我在上面的问题中所说,我已经在我的 servlet-context.xml 上声明了组件扫描,并在我的 root-context.xml 上也尝试了它。我也试过“ph.project.p3”删除.conf,但仍然发生同样的错误,无法自动连接AuthorizationConfiguration。以上是关于创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败的主要内容,如果未能解决你的问题,请参考以下文章
无法修复创建名为“springSecurityFilterChain”的 bean 的错误
无法在 SpringBootApplication 中关闭 SecurityConfig
SecurityConfig 中出现 SpringSecurity + AWS Cognito 连接错误