application-context-security.xml文件中的错误

时间:2014-03-17 05:52:55

标签: java spring spring-mvc spring-security

我正在处理一个项目,但我收到错误“前缀”上下文“for element”上下文:component-scan“未绑定”在xml文件的一边 我的文件代码是

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:security="http://www.springframework.org/schema/security"
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-3.0.xsd">

 <!--Here is i am getting error "The prefix "context" for element "context:component-scan" is not bound."-->
    <context:component-scan base-package="tudu.security"/>

<global-method-security secured-annotations="enabled" />

<HTTP>
    <INTERCEPT-URL PATTERN="/AJAX/**" ACCESS="ROLE_USER" />
    <INTERCEPT-URL PATTERN="/TUDU/WELCOME" ACCESS="ROLE_ANONYMOUS,ROLE_USER" />
    <INTERCEPT-URL PATTERN="/TUDU/RECOVERPASSWORD" ACCESS="ROLE_ANONYMOUS,ROLE_USER" />
    <INTERCEPT-URL PATTERN="/TUDU/REGISTER" ACCESS="ROLE_ANONYMOUS,ROLE_USER" />
    <INTERCEPT-URL PATTERN="/TUDU/ADMIN" ACCESS="ROLE_ADMIN" />
    <INTERCEPT-URL PATTERN="/TUDU/**" ACCESS="ROLE_USER" />
    <INTERCEPT-URL PATTERN="/SERVLET/**" ACCESS="ROLE_USER" />

    <FORM-LOGIN LOGIN-PROCESSING-URL="/TUDU/LOGIN" LOGIN-PAGE="/TUDU/WELCOME"
        AUTHENTICATION-FAILURE-URL="/TUDU/WELCOME?AUTHENTICATION=FAILURE"
        DEFAULT-TARGET-URL="/TUDU/LISTS" ALWAYS-USE-DEFAULT-TARGET="TRUE" />

    <REMEMBER-ME KEY="TUDUROCKS" />
    <LOGOUT LOGOUT-URL="/TUDU/LOGOUT" />
</HTTP>

<AUTHENTICATION-MANAGER ALIAS="AUTHENTICATIONMANAGER">
    <AUTHENTICATION-PROVIDER USER-SERVICE-REF="USERDETAILSSERVICE" />
</AUTHENTICATION-MANAGER>

3 个答案:

答案 0 :(得分:2)

你的xml名称空间需要一些更改

  1. 您缺少上下文命名空间
  2. 您的默认命名空间是bean,看起来您更喜欢将安全性作为默认值
  3. 如果您不使用bean作为默认名称空间,则应将前缀beans:添加到根元素
  4. 我更喜欢用小写字母保存xml名称和属性
  5. 这应该有用。

    <beans:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--Here is i am getting error "The prefix "context" for element "context:component-scan" 
            is not bound." -->
        <context:component-scan base-package="tudu.security" />
    
        <global-method-security secured-annotations="enabled" />
        <http>
            <intercept-url pattern="/AJAX/**" access="ROLE_USER" />
            <intercept-url pattern="/TUDU/WELCOME" access="ROLE_ANONYMOUS,ROLE_USER" />
            <intercept-url pattern="/TUDU/RECOVERPASSWORD" access="ROLE_ANONYMOUS,ROLE_USER" />
            <intercept-url pattern="/TUDU/REGISTER" access="ROLE_ANONYMOUS,ROLE_USER" />
            <intercept-url pattern="/TUDU/ADMIN" access="ROLE_ADMIN" />
            <intercept-url pattern="/TUDU/**" access="ROLE_USER" />
            <intercept-url pattern="/SERVLET/**" access="ROLE_USER" />
    
            <form-login login-processing-url="/TUDU/LOGIN" login-page="/TUDU/WELCOME"
                authentication-failure-url="/TUDU/WELCOME?AUTHENTICATION=FAILURE"
                default-target-url="/TUDU/LISTS" always-use-default-target="true" />
    
            <remember-me key="TUDUROCKS" />
            <logout logout-url="/TUDU/LOGOUT" />
        </http>
        <authentication-manager alias="AUTHENTICATIONMANAGER">
            <authentication-provider user-service-ref="USERDETAILSSERVICE" />
        </authentication-manager>
    </beans:beans>
    

    我正在使用STS提供的弹簧配置编辑器,这让我的生活更轻松。

答案 1 :(得分:1)

命名空间可能存在一些问题...... 你做了什么,直到现在是正确的.....但只是你需要改变一件事 即

将安全性放在默认名称空间中......

只需复制此行:

<beans xmlns="http://www.springframework.org/schema/security"  

并在 bean开启标记

中过去

或者您也可以为所有安全标记使用前缀,例如:

<security:http>

答案 2 :(得分:0)

这是正常的..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<context:component-scan base-package="tudu.security" />

<security:global-method-security
    secured-annotations="enabled" />

<security:http>
    <security:intercept-url pattern="/AJAX/**"
        access="ROLE_USER" />
    <security:intercept-url pattern="/TUDU/WELCOME"
        access="ROLE_ANONYMOUS,ROLE_USER" />
    <security:intercept-url pattern="/TUDU/RECOVERPASSWORD"
        access="ROLE_ANONYMOUS,ROLE_USER" />
    <security:intercept-url pattern="/TUDU/REGISTER"
        access="ROLE_ANONYMOUS,ROLE_USER" />
    <security:intercept-url pattern="/TUDU/ADMIN"
        access="ROLE_ADMIN" />
    <security:intercept-url pattern="/TUDU/**"
        access="ROLE_USER" />
    <security:intercept-url pattern="/SERVLET/**"
        access="ROLE_USER" />

    <security:form-login login-processing-url="/TUDU/LOGIN"
        login-page="/TUDU/WELCOME" authentication-failure-url="/TUDU/WELCOME?AUTHENTICATION=FAILURE"
        default-target-url="/TUDU/LISTS" always-use-default-target="true" />

    <security:remember-me key="TUDUROCKS" />
    <security:logout logout-url="/TUDU/LOGOUT" />
</security:http>

<security:authentication-manager alias="AUTHENTICATIONMANAGER">
<security:authentication-provider user-service-ref="USERDETAILSSERVICE"/>
</security:authentication-manager>

</beans>