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