春季安全无法找到NamespaceHandler

问题描述:

虽然试图建立Spring Security我总是遇到这个错误,我仍然不知道如何处理:春季安全无法找到NamespaceHandler

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [security.xml] 
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security] 
Offending resource: ServletContext resource [/WEB-INF/security.xml]

通过编写此,我严格遵循了Spring Securitydocumentation。在SO中,我也搜索了类似的问题,但没有一个解决了我的问题。

据我看到的,误差主要是由两件事情:一是 ,Spring无法导入的security.xml和第二,Spring无法找到其NamespaceHandler对XML架构命名空间。 我是否错过了一些依赖或为什么Spring找不到它的NamespaceHandler?我正在使用Spring 4.2.0.RELEASESpring Security 4.0.2.RELEASE

所以,这里是我的代码:

的pom.xml

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

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

的security.xml

<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.xsd"> 

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

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

的web.xml

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

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:applicationContext.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> 

而且在applicationContext.xml中我只是进口的security.xml

<import resource="security.xml" /> 

你能还添加以下到您的pom.xml:

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

编辑:

在web.xml中的ContextLoaderListener声明下添加此代码:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:applicationContext.xml 
    </param-value> 
</context-param> 
+0

感谢您的回复;我只是添加了这两个依赖关系,但没有任何改变...仍然有相同的错误。我不明白的是,所需的spring-security-config.jar确实存在于我的库中,但仍然得到了BeanDefinitionParsingException。 – MaxWell

+0

@MaxWell我编辑了我的答案,你可以试试这个新建议 – smoggers

+0

再次感谢...不起作用...。但是现在我得到了一些额外的错误信息:Offending资源:ServletContext资源[/WEB-INF/security.xml]。 – MaxWell