2015-09-05 80 views
0

雖然試圖建立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" /> 

回答

0

你能還添加以下到您的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