【问题标题】:Spring Security XML based configuration : java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"基于 Spring Security XML 的配置:java.lang.IllegalArgumentException:没有为 id“null”映射 PasswordEncoder
【发布时间】:2020-07-03 23:51:46
【问题描述】:

尝试使用基于 spring security 5 的 xml 配置来测试简单的身份验证。 我有这个错误这里是我的 XML 配置文件

spring-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 auto-config="true">
        <intercept-url pattern="/admin"
            access="hasRole('ROLE_ADMIN')" />
    </http>
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="1234"
                    authorities="hasRole(ROLE_ADMIN)" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
    </beans:beans>

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="  http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc.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">
    <mvc:annotation-driven />
    <context:component-scan
        base-package="com.demo.controller">
    </context:component-scan>
    <context:annotation-config></context:annotation-config>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    </beans>   

我应该添加任何配置来支持密码编码吗? 这是我的控制器

package com.demo.controller;

   import org.springframework.stereotype.Controller;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RequestMethod;

     @Controller
     public class HomeController {

     @RequestMapping(value = "/", method = RequestMethod.GET)
     public String home() {
        return "home";
     }

     @RequestMapping(value = "/admin", method = RequestMethod.GET)
     public String privateHome() {
        return "privatePage";
     }
    }

对于我的 web.xml

 <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE xml>
     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">

    <!-- Spring Configuration -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring-servlet.xml
            /WEB-INF/spring-security.xml
        </param-value>
     </context-param>
    </web-app> 

我正在使用 spring mvc 5.0.2.RELEASE 谢谢。

【问题讨论】:

标签: java spring spring-security


【解决方案1】:

对于那些面临同样问题的人,解决方案是将此配置添加到 spring-securit。 xml

<authentication-manager>
<authentication-provider>
    <password-encoder hash="bcrypt" />
</authentication-provider>

之后,身份验证应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 2018-09-14
    • 2021-06-02
    • 2020-08-11
    • 2018-11-14
    • 2018-10-30
    • 2019-06-02
    • 2020-06-24
    相关资源
    最近更新 更多