Spring Security 报There is no PasswordEncoder mapped for the id "null"

原代码为:

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("rg2")
            .password("123456")
            .roles("ADMIN");
}

记过发现报错Spring Security 报There is no PasswordEncoder mapped for the id "null"

原因是Spring Security 升级到5版本后密码支持多种加密格式;

添加一个新的类

public class MyPasswordEncoder implements PasswordEncoder{

    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }

}

然后再原代码中改为

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

 

posted @ 2018-06-10 16:52  十黎九夏  阅读(4673)  评论(0编辑  收藏  举报