gpt4 book ai didi

java - 未定义名为 'springSecurityFilterChain' 的 bean

转载 作者:行者123 更新时间:2023-12-01 12:41:50 25 4
gpt4 key购买 nike

我在设置 Spring Security 时遇到一些问题似乎找不到 springSecurityFilterChain bean我的 Spring 安全配置:

<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/security"
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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

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

<authentication-manager>
<authentication-provider>
<user-service>
<user name="user1" password="1234" authorities="ROLE_USER" />
</user-service>
</authentication-provider>

</authentication-manager>

</beans:beans>

我的 web.xml

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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/security-config.xml
</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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

<!-- 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>

</web-app>

我的 xml 文件位置:

enter image description here

我认为它找不到/WEB-INF/config/security-config.xml

最佳答案

我认为问题的原因可能是启动 Web 应用程序时未加载 Spring Security 的 xml 配置文件。

要解决此问题,您应该在 web.xml 中指定所有 XML 配置文件,如下所示:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>

如果您的配置文件位于classpath(不是WEB-INF文件夹或其子文件夹)中,那么您可以以这种方式指定配置文件列表;

<param-value>
classpath:applicationContext.xml,
classpath:spitter-security.xml
</param-value>

您还需要添加特殊的监听器来加载您的配置文件:

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

关于java - 未定义名为 'springSecurityFilterChain' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com