2

我找到了一个基本的弹簧安全示例,但我通常无法在 tomcat 上运行它。我知道此错误消息可能是由配置 xml 文件引起的,但我找不到它。谢谢您的帮助:)

1. Error Message

java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
        at java.lang.Class.getDeclaredMethods(Class.java:1845)
        at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotated
Methods(StandardAnnotationMetadata.java:136)
        at org.springframework.context.annotation.ConfigurationClassBeanDefiniti
onReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader
.java:318)

2. File Structure

在此处输入图像描述

3. web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <display-name>Spring MVC Application</display-name>

    <!-- Spring MVC -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml,
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <!-- 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>

</web-app>

4. mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <context:component-scan base-package="com.mkyong.common.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>

</beans>

5.spring-security.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    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="/welcome*" access="ROLE_USER" />

        <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed"
             />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="mkyong" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>
4

3 回答 3

3

这是您的一部分stacktrace

Constructor threw exception; nested exception is java.lang.NoC
lassDefFoundError: org/springframework/aop/config/AbstractInterceptorDrivenBeanD
efinitionDecorator

这表示Aspect Oriented Programming (AOP) Frameworkjar 中不存在classpath. 因此,您需要将spring-aop(spring-aop-3.0.5.RELEASE.jar) jar 添加到您的classpath.

于 2013-10-14T03:10:38.173 回答
1

您需要将 spring.jar 添加到您的库中。spring-security jars 将引用它。因此初始化失败。

于 2013-10-14T02:53:13.373 回答
1

我在我的项目中发现了同样的异常,但是在添加 spring-aop-4.1.6.RELEASE.jar 之后我解决了这个问题。请注意要在项目中添加的 jar 版本... :)

于 2016-09-17T17:13:13.453 回答