spring 获取异常:未定义名为“springSecurityFilterChain”的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12123516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting exception: No bean named 'springSecurityFilterChain' is defined
提问by Pokuri
I am learning spring security from reference material. release 3.1.2.RELEASE. As stated in that I have configured security:httptag like this
我正在从参考资料中学习弹簧安全性。版本 3.1.2.RELEASE。如前所述,我已经配置了这样的security:http标签
security-context.xml
安全上下文.xml
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
web.xml
网页.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:*-context.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>security</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>security</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
security-servlet.xml
安全servlet.xml
<context:component-scan base-package="com.pokuri.security.mvc.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
But I am getting this exception when I start the application. If I remove security configuration my spring web application working fine. I went through the same kind of questions in stackoverflow. But no luck.
但是当我启动应用程序时出现此异常。如果我删除安全配置,我的 Spring Web 应用程序工作正常。我在 stackoverflow 中遇到了同样的问题。但没有运气。
回答by dimas
I think that the reason of your problem can be in that your xml configuration file for spring security isn't loaded when you start your web app.
我认为您的问题的原因可能是您启动 Web 应用程序时未加载用于 spring 安全性的 xml 配置文件。
To fix this you should specify all your XML config files in web.xml like that:
要解决此问题,您应该在 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>
If you have your config files in classpath (not WEB-INF folder or it's subfolders) then you can specify list of config files in such way;
如果您的配置文件在类路径(不是 WEB-INF 文件夹或其子文件夹)中,那么您可以以这种方式指定配置文件列表;
...
<param-value>
classpath:applicationContext.xml,
classpath:spitter-security.xml
</param-value>
...
And also you need to add special listener that will load your config files:
您还需要添加特殊的侦听器来加载您的配置文件:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
回答by LottaLava
I just added the bean definition in applicationContext.xml as Spring asked:
我刚刚按照 Spring 的要求在 applicationContext.xml 中添加了 bean 定义:
<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>
回答by Patrikoko
add this your web.xml
将此添加到您的 web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.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>
<!-- filter declaration for Spring Security -->
<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>
回答by achAmháin
In case it helps anyone, I had renamed one of my packages but Eclipse doesn't auto-update your @ComponentScanpaths, so make sure you change that too:
如果它对任何人有帮助,我已经重命名了我的一个包,但 Eclipse 不会自动更新您的@ComponentScan路径,因此请确保您也更改它:
@ComponentScan(basePackages = "com.package.spring")
回答by Hegdekar
As of Spring Security 5.2.1-SNAPSHOT, this error occurred to me when I hadn't declared the <http/>element in security XML configuration.
从 Spring Security 5.2.1-SNAPSHOT 开始,当我没有<http/>在安全 XML 配置中声明元素时,我发生了这个错误。
I was trying a sample and I had configuration like
我正在尝试一个示例,我的配置如下
<b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">
<user-service>
<user name="user" password="{noop}password" authorities="ROLE_USER" />
</user-service>
</b:beans>
I had to change it to add <http/>like below.
我不得不改变它添加<http/>如下。
<b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">
<http />
<user-service>
<user name="user" password="{noop}password" authorities="ROLE_USER" />
</user-service>
</b:beans>

