找不到SAXParseException前缀

时间:2014-01-08 09:14:15

标签: xml spring xml-namespaces

我希望将使用spring的应用程序与LDAP集成,但是当我尝试测试时出现错误。

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

1 个答案:

答案 0 :(得分: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命名空间中的元素。