java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”

最近在学习Spring全家桶中的Spring Security安全框架,运用的是 5.x.x 版本,于是出现了如下问题:java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”

解决方法:实现一个 PassordEncoder接口,然后在 继承了 WebSecurityConfigurerAdapter的配置类里面配置默认的账号信息即可;

public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();
    }
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return encodedPassword.equals(rawPassword.toString());
    }
}

下面代码,添加到安全框架的自定义配置类里:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .passwordEncoder(new MyPasswordEncoder())
            .withUser("admin")
            .password("123456")
            .roles("ADMIN");
    }

参考:https://blog.csdn.net/weixin_39220472/article/details/80865411


185 Clicks !