No bean named 'springSecurityFilterChain' is defined

1.问题

本文讨论Spring安全配置问题 - 应用程序引导过程抛出如下异常:web

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:spring

<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配置没有定义<http>元素:app

<?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命名空间 - 如上例所示,则声明一个简单的<http>元素将确保建立过滤器bean而且全部内容都正确启动:url

<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

另外一个可能的缘由是Security配置根本不会导入到Web应用程序的总体上下文中。spa

若是安全性XML配置文件名为springSecurityConfig.xml,请确保导入资源:代理

@ImportResource({"classpath:springSecurityConfig.xml"})

或者用XML:code

<import resource="classpath:springSecurityConfig.xml" />

最后,能够在web.xml中更改过滤器bean的默认名称 - 一般使用带有Spring Security的现有过滤器:xml

<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 - 并展现了这个常见问题的解决方案get

相关文章
相关标签/搜索