java - 发现以元素 'authentication-manager' 开头的无效内容

标签 java spring spring-mvc

我的 spring 配置出现错误:

Invalid content was found starting with element 'authentication-manager'. One of '{"http://www.springframework.org/schema/beans":import, "http://www.springframework.org/schema/beans":alias, "http://www.springframework.org/schema/beans":bean, WC[##other:"http://www.springframework.org/schema/beans"]}' is expected.

这是 bean xml 文件:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.rsc."/>
    <mvc:annotation-driven />

    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userDetailsServiceImpl">
            <password-encoder ref="encoder"></password-encoder>
        </authentication-provider>
    </authentication-manager>

    <bean id="userDetailsServiceImpl" class="com.rsc.service.UserDetailsServiceImpl"></bean>

    <bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <constructor-arg name="strength" value="11"/>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我已经添加了所有必需的 bean 配置,但仍然收到错误。我在配置中缺少什么?

最佳答案

我认为您需要添加 spring security 命名空间。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:security="http://www.springframework.org/schema/security"         
   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/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.1.xsd">

   [...]

   <security:authentication-manager>
      ...
   </security:authentication-manager>  

   [...]

</beans>

因为 beans 是你的默认命名空间,spring 尝试在那里找到 authentication-manager 。因此,您需要引入 security 命名空间。有关更多信息,请参阅文档:http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#d0e507

关于java - 发现以元素 'authentication-manager' 开头的无效内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39816576/

相关文章:

javascript - 在 Ajax 调用中返回实体对象而不是字符串

spring-mvc - web.xml 中的 Spring servlet 缺少 contextConfigLocation 参数值

java - Spring ResponseBodyEmitter 未在响应中设置 Content-Type header

spring - Grails:SpringSecurity角色层次结构未按预期工作

java - 如何使实体中的列不可读取

java - JSON 响应中的重复字段

spring-mvc - 可完成的 future : control http response status-codes

java - 正则表达式 - 否定特定的字符串/数字序列 [java]

java - 当进度条达到某个值时更改 jLabel 文本

Java 处理小程序未运行