No bean named 'springSecurityFilterChain' is defined
2019-05-19 22:51
405 查看
1.问题
本文讨论Spring安全配置问题 - 应用程序引导过程抛出以下异常:
SEVERE: Exception starting filter springSecurityFilterChain org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
2.原因
此异常的原因很简单 - Spring Security查找名为springSecurityFilterChain的bean(默认情况下),但无法找到它。主要的Spring安全过滤器 - DelegatingFilterProxy - 在web.xml中定义了这个bean:
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这只是一个代理,它将其所有逻辑委托给springSecurityFilterChain bean。
3.解决方案
上下文中缺少此bean的最常见原因是security XML配置没有定义元素:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> </beans:beans>
如果使用XML配置Security命名空间 - 如上例所示,则声明一个简单的元素将确保创建过滤器bean并且所有内容都正确启动:
<http auto-config='true'> <intercept-url pattern="/**" access="ROLE_USER" /> </http>
另一个可能的原因是Security配置根本不会导入到Web应用程序的整体上下文中。
如果安全性XML配置文件名为springSecurityConfig.xml,请确保导入资源:
@ImportResource({"classpath:springSecurityConfig.xml"})
或者用XML:
<import resource="classpath:springSecurityConfig.xml" />
最后,可以在web.xml中更改过滤器bean的默认名称 - 通常使用带有Spring Security的现有过滤器:
<filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>customFilter</param-value> </init-param> </filter>
4.总结
本文讨论了一个非常具体的Spring Security问题 - 缺少过滤器链bean - 并展示了这个常见问题的解决方案。
相关文章推荐
- SpringMVC中ApplicationContext中的配置文件的问题(No bean named 'sessionFactory' is defined 已解决)
- 项目“No bean named 'shiroFilter' is defined”错误处理
- No bean named 'sessionFactory' is defined
- No qualifying bean of type [com.exe.demo01.ch1.di.UseFunctionService] is defined
- No bean named 'sessionFactory' is defined
- 关于No bean named 'sessionFactory' is defined的诡异事件
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cxf' is defined
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'XXXX' is defined
- org.springframework.beans.factory.BeanCreationException: Error creating (no bean "" is defined)
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'depDao' is defined
- No bean named 'sessionFactory' is defined
- No bean named 'uMapper' is defined
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ExtractAbstractServiceHandler' is defined
- 使用mvc:annotation-driven时遇到的No bean named 'cacheManager' is defined问题
- Noqualifying bean of type [org.apache.solr.client.solrj.SolrServer] is defined:expected single match
- No qualifying bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single
- No bean named 'sessionFactory' is defined
- 异常问题 No bean named 'sessionFactory' is defined
- No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
- No bean named 'cacheManager' is defined