2014-01-08 28 views
1

我想集成我的應用程序與春與LDAP一起工作,但是當我嘗試測試我有一個錯誤。SAXParseException前綴找不到

安全-APP-context.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-3.0.xsd 
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <http auto-config="true" use-expressions="true"> 
     <intercept-url pattern="/test/**" access="hasRole('ADMIN')" /> 
     <intercept-url pattern="/test1/**" access="hasRole('USER')" /> 

     <form-login login-page="/index.htm" authentication-success-handler-ref="authenticationSuccessRedirecthandler" 
      default-target-url = "/test/MainHealthCertificat.htm" 
      authentication-failure-url="/index.htm?error=1"/> 
     <logout logout-success-url="/index.htm" /> 
    </http> 

    <beans:bean class="com..CustomAuthenticationHandler" id="authenticationSuccessRedirecthandler"></beans:bean> 

     <authentication-manager> 
     <authentication-provider> 
       <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username, password, enabled from users where username=?" 
        authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur where u.user_id = ur.user_id and u.username =? " 
       /> 
     </authentication-provider> 
    </authentication-manager> 
</beans:beans> 

我嘗試使用這個配置來修改此文件:

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

     <http auto-config="true" use-expressions="true"> 
      <intercept-url pattern="/test/**" access="hasRole('ADMIN')" /> 
      <intercept-url pattern="/test1/**" access="hasRole('USER')" />       
      <form-login login-page="/index.htm" authentication-success-handler-ref="authenticationSuccessRedirecthandler" 
       default-target-url = "/test/MainHealthCertificat.htm" 
       authentication-failure-url="/index.htm?error=1"/> 
      <logout logout-success-url="/index.htm" /> 
     </http> 

     <beans:bean class="com..CustomAuthenticationHandler" id="authenticationSuccessRedirecthandler"></beans:bean> 

    <security:authentication-manager> 
     <security:ldap-authentication-provider 
      user-search-filter="(uid={0})" 
      user-search-base="ou=users" 
      group-search-filter="(uniqueMember={0})" 
      group-search-base="ou=groups" 
      group-role-attribute="cn" 
      role-prefix="ROLE_"> 
     </security:ldap-authentication-provider> 
    </security:authentication-manager> 

    <security:ldap-server url="ldap://192.168.0.42:389" manager-dn="uid=admin,ou=system" manager-password="secret" /> 
</beans:beans> 

,但是當我測試我有這樣的錯誤:

Caused by: org.xml.sax.SAXParseException: The prefix "security" for element "security:authentication-manager" is not bound. 
    at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) [xerces_2_5_0.jar:] 
    at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) [xerces_2_5_0.jar:] 
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE] 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE] 
    ... 21 more 

回答

0

在這兩個示例配置文件中,默認名稱空間是xmlns="http://www.springframework.org/schema/security"這意味着您不帶前綴的XML元素將被解析爲該名稱空間,例如<authentication-manager>

xmlns:beans="http://www.springframework.org/schema/beans"其他命名空間聲明元素,你想從http://www.springframework.org/schema/beans命名空間必須beans前綴使用。所以你可能寫了xmlns:coolbeans="http://www.springframework.org/schema/beans",但是那個名字空間的元素需要加上前綴coolbeans,比如<coolbeans:bean class="com..CustomAuthenticationHandler" id="authenticationSuccessRedirecthandler"></coolbeans:bean>

第二個示例不起作用的原因是已添加security名稱空間前綴,例如<security:authentication-manager>,但沒有用該前綴聲明的名稱空間。

要解決該問題,請刪除security:前綴,因爲名稱空間的http://www.springframework.org/schema/security中的元素的默認名稱空間已存在。

+0

謝謝你,問題解決了 – franco

+0

在當前你問的36個問題中,有11個有答案,沒有一個被接受。我可以請建議閱讀關於堆棧溢出如何工作 - http://stackoverflow.com/help/someone-answers – andyb