問題描述:
使用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"); }