org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为“springSecurityFilterChain”的 bean - 基于 Java 的配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25314941/
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
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined - Java Based Configuration
提问by Thunder
Trying to resolve org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
in Java based configuration.
尝试org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
在基于 Java 的配置中解决。
web.xml
网页.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"
id="WebApp_ID" version="3.0">
<!-- 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>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.examples.config.WebAppConfig</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>
<!-- 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>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
</web-app>
The WebAppCinfig java based:
基于Java的WebAppCinfig:
@EnableWebMvc
@Configuration
@Import(value = SecurityConfig.class)
@ComponentScan(basePackages = {"com.examples"})
@ImportResource({ "classpath:spring-security-config.xml" })
public class WebAppConfig extends WebMvcConfigurerAdapter {
public WebAppConfig() {
super();
}
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/anonymous.html");
registry.addViewController("/login.html");
registry.addViewController("/homepage.html");
}
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
}
The SecurityConfig.java file for the security:
安全性的 SecurityConfig.java 文件:
@Configuration
@ImportResource({ "classpath:spring-security-config.xml" })
public class SecurityConfig {
public SecurityConfig() {
super();
}
}
And finally the WebAppConfig.java class:
最后是 WebAppConfig.java 类:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
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/security
http://www.springframework.org/schema/security/spring-security.xsd">
<http auto-config="true">
<intercept-url pattern="/anonymous*" access="isAnonymous()"/>
<intercept-url pattern="/login*" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()"/>
<form-login login-page='/login.html' login-processing-url="/perform_login"
default-target-url="/homepage.html" authentication-failure-url="/login.html?error=true"
always-use-default-target="true"/>
<logout logout-url="/perform_logout" delete-cookies="JSESSIONID"
success-handler-ref="customLogoutSuccessHandler"/>
</http>
<beans:bean name="customLogoutSuccessHandler"
class="com.examples.config.CustomLogoutSuccessHandler"/>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="user1" password="pass1" authorities="ROLE_USER"/>
<user name="user2" password="pass2" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
Now, I am getting org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
in a Java base Spring Security project I am working on. Things works on Annotation Configuration well and is testable in a great way. For some reason, Spring does is throwing such an error while trying to accomplish Spring Security Form Login example.
现在,我进入org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
了我正在从事的基于 Java 的 Spring Security 项目。事情在注释配置上运行良好,并且可以很好地进行测试。出于某种原因,Spring 在尝试完成 Spring Security Form Login 示例时确实抛出了这样的错误。
Also in the spring-security-config.xml tried changing <http use-expressions="true">
to <http auto-config="true">
but still no luck resolving No Bean Named 'springSecurityFilterChain'
found exception.
同样在 spring-security-config.xml 中尝试更改<http use-expressions="true">
为<http auto-config="true">
但仍然没有成功解决 No Bean Named 'springSecurityFilterChain'
found 异常。
Any help in this matter would be highly appreciated. Thanks,
在这方面的任何帮助将不胜感激。谢谢,
回答by Prime
Spring container is trying to find springSecurityFilterChain bean in application context, not web application context. Change spring security configuration to be included in root context.
Spring 容器试图在应用程序上下文中找到 springSecurityFilterChain bean,而不是 Web 应用程序上下文。将 spring 安全配置更改为包含在根上下文中。
回答by Victor
Please provide minimal configuration of spring-security
请提供 spring-security 的最低配置
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
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.0.3.xsd">
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>