[英]Spring-based Application not loading due to misconfiguration
I am developing an spring application based in a code from a tutorial, with the objective of learn about spring-security using database. 我正在基于教程中的代码开发spring应用程序,目的是学习使用数据库的spring-security。 My application, in this moment, is displaying an erro 404 when I try run it from Eclipse with Tomcat7.
当我尝试从Tomcat7的Eclipse运行它时,我的应用程序此时显示错误404。 While in the browser a 404 error page is displayed, in the console of the Eclipse the follow message is observed:
在浏览器中显示404错误页面时,在Eclipse的控制台中,观察到以下消息:
org.springframework.beans.factory.BeanCreationException: 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]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'org.springframework.security.provisioning.JdbcUserDetailsManager#0' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.provisioning.JdbcUserDetailsManager#0': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
My configuration files are listed below: 我的配置文件如下所示:
web.xml 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">
<display-name>sdnext</display-name>
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdnext-servlet.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdnext-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
<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>
sdnext-servlet.xml sdnext-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties" />
<context:component-scan base-package="com.dineshonjava.security" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
</beans>
sdnext-security.xml sdnext-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"
xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd">
<security:http auto-config="true" >
<security:intercept-url pattern="/index" access="ROLE_USER" />
<security:intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:form-login login-page="/login" default-target-url="/index" authentication-failure-url="/fail2login" />
<security:logout logout-success-url="/logout" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username, password, active from users where username=?"
authorities-by-username-query="select us.username, ur.authority from users us, user_roles ur
where us.user_id = ur.user_id and us.username =? "
/>
</security:authentication-provider>
</security:authentication-manager>
</beans>
database.properties database.properties
database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/DAVDB?charSet=LATIN1
database.user=***
database.password=***
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
My JARS included in this project: 我的JARS包含在此项目中:
**aopalliance-1.0.jar
commons-logging-1.1.1.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
postgresql-9.3-1100.jdbc3.jar
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-jdbc-3.2.4.RELEASE.jar
spring-orm-3.2.4.RELEASE.jar
spring-security-config-3.2.0.RELEASE.jar
spring-security-core-3.2.0.RELEASE.jar
spring-security-web-3.2.0.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
spring-web-3.2.4.RELEASE.jar
spring-webmvc-3.2.4.RELEASE.jar**
Someone can identify any error in this files? 有人可以识别此文件中的任何错误吗? I spent many time with them, and still can figure what's wrong.
我花了很多时间和他们在一起,但仍然可以找出问题所在。
You can't have two context parameter named contextConfigLocation
in your web.xml
. 您的
web.xml
不能有两个名为contextConfigLocation
上下文参数。 To declare multiple context configuration files, you must seperate them with a space. 要声明多个上下文配置文件,必须用空格分隔它们。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/sdnext-servlet.xml
/WEB-INF/sdnext-security.xml
</param-value>
</context-param>
If you look at the last line of the stacktrace, it says:- 如果您查看stacktrace的最后一行,它会说:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.security.provisioning.JdbcUserDetailsManager#0': Cannot resolve
reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
It seems like you defined that bean in the wrong file. 似乎您在错误的文件中定义了该bean。
So, move dataSource
bean from sdnext-servlet.xml
to sdnext-security.xml
first. 因此,首先将
dataSource
bean从sdnext-servlet.xml
移到sdnext-security.xml
。
In your web.xml, you already have the following configuration:- 在您的web.xml中,您已经具有以下配置:
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
Since the DispatcherServlet's servlet-name
is sdnext
, by default, Spring MVC will automatically look for sdnext-servlet.xml
under /WEB-INF
folder. 由于DispatcherServlet的
servlet-name
默认为sdnext
,因此Spring MVC将自动在/WEB-INF
文件夹下查找sdnext-servlet.xml
。 That said, you can safely remove this configuration below from web.xml because 1) Spring MVC is already finding that configuration file by itself and 2) contextConfigLocation
is used only by Spring, not Spring MVC, so there's no point exposing servlet related configuration here:- 就是说,您可以从web.xml中安全地删除此配置,因为1)Spring MVC已经自行找到该配置文件,并且2)
contextConfigLocation
仅由Spring使用,而不是Spring MVC使用,因此这里没有公开与servlet相关的配置:-
<!-- Delete this! -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdnext-servlet.xml</param-value>
</context-param>
So, your web.xml should have exactly one contextConfigLocation
pointing to /WEB-INF/sdnext-security.xml
, which you already have. 因此,您的web.xml应该恰好具有一个指向
/WEB-INF/sdnext-security.xml
contextConfigLocation
,它已经具有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.