错误使用JDBC的Spring3 Security上下文初始化失败。
文件:applicationContext-security-JDBC.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config="true">
<intercept-url pattern="/friends/**" access="ROLE_USER" />
<intercept-url pattern="/articles/**" access="ROLE_USER" />
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource" />
</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">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-security-JDBC.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>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Publishes events for session creation and destruction through the application
- context. Optional unless concurrent session control is being used.
-->
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
以下是错误:
[ERROR,ContextLoader]上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authentication.ProviderManager#0'的bean时出错:无法创建[org.springframework.security.config类型的内部bean'(内部bean)'。 authentication.AuthenticationManagerFactoryBean]设置bean属性'parent';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)'的bean时出错:FactoryBean在创建对象时抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authenticationManager'的bean时出错:设置时无法解析bean'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的引用带有键[0]的bean属性'providers';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的bean时出错:无法解析对bean的引用'org.springframework.security.provisioning.JdbcUserDetailsManager#设置bean属性'userDetailsService'时为0';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.security.provisioning.JdbcUserDetailsManager#0'的bean时出错:在设置bean属性'dataSource'时无法解析对bean'DataSource'的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'dataSource'的bean
答案 0 :(得分:2)
这很容易回答。异常消息说(最后)“没有定义名为'dataSource'的bean”。当然,您的applicationContext-security-JDBC.xml
文件不包含dataSource
bean的定义。
SpringSecurity手册的6.2节包含有关如何为JDBC用户详细信息服务配置数据源的资料。 (提示:SpringSecurity需要告诉使用什么数据库,用户名和密码...)