无法读取架构文档 'http://www.springframework.org/schema/security/spring-security-4.0.xsd'

Failed to read schema document 'http://www.springframework.org/schema/security/spring-security-4.0.xsd'

我想使用 Spring 这样配置的安全性

<?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.xsd
                    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">

    <http>
        <intercept-url pattern="/add-job**" access="hasRole('USER')" />
        <form-login />
        <logout />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
            <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

但我收到错误

Multiple annotations found at this line: - schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/ security/spring-security-4.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not . - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'http'.

我的 pom.xml 安全是

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

我找不到解决这个问题的方法。

Spring 建议使用此 http://www.springframework.org/schema/security/spring-security.xsd 而不是指定版本号,就像使用 spring-beans.

一样

这样,我有一个具有相同 spring 安全依赖项的项目,但是 4.0.2.RELEASE 我有 4.0.1.RELEASE 并且它没有问题。

因此您必须按如下方式尝试架构配置:

xsi:schemaLocation="
   http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
   http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd"

如果它不起作用,那么您可能存在传递依赖性问题,您只是加载了两个或更多不同的 spring 安全版本。在这种情况下,请尝试使用 mvn dependency:tree 查看 configuration 工件上是否有多个 spring-security depedency,并排除不需要的那个。

感谢回答者,但最终我在 Migrating from Spring Security 3.x to 4.x (XML Configuration) 找到了解决方案,并找到了以下依赖项

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>4.0.0.RELEASE</version>
</dependency>

这与问题中提到的一起,这种方法解决了我的问题。