Java 使用键 [0] 设置 bean 属性“sourceList”时,无法解析对 bean“org.springframework.security.web.DefaultSecurityFilterChain#0”的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21624299/
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
cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]
提问by arpho
I have to add spring security 3.2 to my application, this are my configuration files:
我必须将 spring security 3.2 添加到我的应用程序中,这是我的配置文件:
Spring-security.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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- This is where we configure Spring-Security -->
<security:http auto-config="true" use-expressions="true" access-denied- page="/auth/denied" >
<security:intercept-url pattern="/auth/login" access="permitAll"/>
<security:intercept-url pattern="/main/admin" access="hasRole('ROLE_ADMIN')"/>
<security:intercept-url pattern="/**" access="ROLE_USER" />
<security:form-login
login-page="/auth/login"
authentication-failure-url="/auth/login?error=true"
default-target-url="/main/common"/>
<security:logout
invalidate-session="true"
logout-success-url="/auth/login"
logout-url="/auth/logout"/>
</security:http>
<!-- Declare an authentication-manager to use a custom userDetailsService -->
<security:authentication-manager>
<security:authentication-provider user-service-ref="customUserDetailsService">
<security:password-encoder ref="passwordEncoder"/>
</security:authentication-provider>
</security:authentication-manager>
<!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>
<!-- A custom service where Spring will retrieve users and their corresponding access levels -->
<!-- bean id="customUserDetailsService" class="com.metmi.mmasgis.tutorial.service.CustomUserDetailsService"/-->
web.xml
网页.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">
<!-- 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
/WEB-INF/spring/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>
<!-- Processes application requests -->
<servlet>
<servlet-name>mmasgisServlet</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>mmasgisServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
</web-app>
Running the application I get this error:Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0];
运行应用程序我收到此错误:Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0];
here you can see the full trace : http://pastebin.com/qfqq6Vdy
在这里你可以看到完整的跟踪:http: //pastebin.com/qfqq6Vdy
采纳答案by Rob Winch
Based upon the logs seems that a bean with the id 'customUserDetailsService' is not defined.
根据日志,似乎未定义 ID 为“customUserDetailsService”的 bean。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customUserDetailsService'
I see that your config you posted does have a bean named customUserDetailsService so I would check a few things.
我看到您发布的配置确实有一个名为 customUserDetailsService 的 bean,因此我会检查一些内容。
- First ensure the configuration you posted matches your actual configuration.
- The configuration has the security configuration and the UserDetailsService in a single file. Is this actually how you have it configured?
- Are you using Global Method Security? If so, this has been known to trigger bugs with eager instantiation of beans relating to authentication. To figure it out, I will need to see this configuration as well.
- Randomly, try moving the customUserDetailsService bean before the rest of your security configuration. This really should not matter, but is more of an experiment.
- 首先确保您发布的配置与您的实际配置相匹配。
- 该配置在单个文件中包含安全配置和 UserDetailsService。这实际上是你配置的方式吗?
- 您是否在使用全局方法安全性?如果是这样,已知这会触发与身份验证相关的 bean 的急切实例化的错误。为了弄清楚,我还需要查看此配置。
- 随机尝试在其他安全配置之前移动 customUserDetailsService bean。这真的应该无关紧要,但更像是一个实验。