解決java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"


問題描述:

使用springboot,權限管理使用spring security,使用內存用戶驗證,但無響應報錯:

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

解決方式

①:

創建MyPasswordEncoder類實現PasswordEncoder,加注解  @Component

@Component
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());
    }
}

 

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //如果程序報錯There is no PasswordEncoder mapped for the id "null",就將該段注釋添加下邊代碼
        //或者在MyPasswordEncoder類上加一個@Component注解,使它成為一個Bean
        auth.inMemoryAuthentication().withUser("admin").password("123456").authorities("ADMIN_ADD","ADMIN_FIND");//自定義登錄用戶用戶名和密碼並賦予一些權限
    }

②:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        

        //這個是使用了匿名內部類
        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("lxy").password("lxy").authorities("ADMIN_ADD","ADMIN_FIND");
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM