java - Spring Security "No bean named ' filterChainProxy'可用“错误

标签 java xml spring spring-mvc spring-security

我按照几个教程来使用 Spring security,但仍然遇到相同的“没有名为 'filterChainProxy' 的 bean available”问题。

org.apache.catalina.core.StandardContext.filterStart Exception starting filter [filterChainProxy]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filterChainProxy' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210)

我使用以下配置:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     version="3.0">

<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>

<filter>
    <filter-name>filterChainProxy</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>filterChainProxy</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>

applicationContext-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

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

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="blabla" password="123456" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

spring-mvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="com.medkhelifi.tutorials.spring.springregistration"/>
<context:annotation-config/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

我不知道我错过了什么。 谢谢。 我用: Spring :5.0.5.RELEASE Spring-Security:3.2.5

最佳答案

Spring Security 注册一个名为 springSecurityFilterChain 的 bean如 Spring Security Reference Guide 中所述。

DelegatingFilterProxy 默认情况下,将尝试检测与过滤器名称同名的 bean。在你的情况下filterChainProxy 。然而,这会失败,因为如上所述,创建了一个名为 springSecurityFilterChain 的 bean。 .

现在您可以做两件事来修复它

  1. 将过滤器重命名为 springSecurityFilterChain如上所述here
  2. 包含在<alias name="filterChainProxy" alias="springSecurityFilterChain"/>中如果您无法修改web.xml

关于java - Spring Security "No bean named ' filterChainProxy'可用“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50171509/

相关文章:

xml - 如何使用 Excel VBA 导入 XML 数据?

java - 解析没有根元素的 XML 流

java - 如何延迟注册 Spring 'bean definitions' 以缩短启动时间?

spring - 如何在 Spring boot 中使用属性文件添加 Cassandra MaxRequestsPerConnection

Spring 重试模板不适用于 @org.springframework.transaction.annotation.Transactional

java - Sling Rewriter 更改静态资源 URL

java - 如何用Spring注入(inject)MessageSource资源包?

xml - 具有属性的 XML 元素的 LINQ To XML 语法

java - 如何获取JDOM2中CDATA的值

java - 适配器中的空指针异常