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");
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM