1

我想在 Spring Security 中委托我的项目的安全性,但是当我将依赖项添加到我的 POM 并启动服务器时,我得到一个没有意义的错误......它说我的 applicationContext.xml 有一个错误并且它指向我有一个 aop 语句的行...

这是我的 applicationContext.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:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop" 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.1.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

 <!-- Activates scanning of @Autowired -->
    <context:annotation-config/>

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="es.myproject"/>

    <!-- datasource configuration -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${jndi.name}" />
        <property name="lookupOnStartup" value="true"></property>
        <property name="cache" value="true"></property>
        <property name="proxyInterface" value="javax.sql.DataSource"></property>
    </bean>

    <!-- hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource"  ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="packagesToScan" value="es.myproject.modelo.datos" />
    </bean>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <aop:aspectj-autoproxy />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

这些是 de 依赖项:

<!-- Spring security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.1.0.RELEASE</version>    

        </dependency> 
       <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.1.0.RELEASE</version> 
        </dependency>   
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.1.0.RELEASE</version>
        </dependency>

并且错误指向<aop:aspectj-autoproxy />applicationContext.xml

如果我删除 Spring Security 依赖项,错误就会消失......很奇怪吧?有什么想法吗?

错误说: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/aop/spring-aop-3.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 41 in XML document from ServletContext resource [/WEB-INF/classes/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:aspectj-autoproxy'.

但是阅读那个xsd并没有真正的问题,正如我所说,如果我删除spring安全依赖项,这个错误不会显示......

4

1 回答 1

3

奇怪的。您的 XML 是有效的(根据 W3 验证器),并且您的 bean 定义看起来都很合理。这让我认为这是一个类路径问题。确保您的类路径中有以下 jar:

  • aopalliance-1.0.jar
  • commons-logging-1.0.1.jar
  • spring-aop-3.1.0.RELEASE.jar
  • spring-asm-3.1.0.RELEASE.jar
  • spring-beans-3.1.0.RELEASE.jar
  • spring-context-3.1.0.RELEASE.jar
  • spring-core-3.1.0.RELEASE.jar
  • spring-expression-3.1.0.RELEASE.jar
  • spring-jdbc-3.1.0.RELEASE.jar
  • spring-orm-3.1.0.RELEASE.jar
  • spring-security-core-3.1.0.RELEASE.jar
  • spring-security-web-3.1.0.RELEASE.jar
  • spring-tx-3.1.0.RELEASE.jar
  • spring-web-3.1.0.RELEASE.jar

还要确保这些都不会出现多次(可能使用不同的版本)

啊,重新阅读您的问题,问题可能是您没有明确将 Spring 3.1.x 列为依赖项。Spring Security 3.1.x 仅拉取 Spring core 3.0.7,除非您另有明确要求。

添加这些依赖项,您应该是安全的:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.1.0.RELEASE</version>
</dependency>
于 2012-07-09T09:23:06.260 回答