Problem Description:
Use springboot, use spring security for permission management, use memory user authentication, but no response, error report:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null”
Solutions
①Create mypasswordencoder class, implement passwordencoder, and annotate @ 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 {
//If the program reports an error There is no PasswordEncoder mapped for the id "null", comment the paragraph and add the following code
//or add a @Component annotation to MyPasswordEncoder class to make it a bean
auth.inMemoryAuthentication().withUser("admin").password("123456").authorities("ADMIN_ADD", "ADMIN_FIND");// customize the login user name and password and give some permissions
}
②:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//This one uses the anonymous internal class
auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("lxy").password("lxy").authorities("ADMIN_ADD","ADMIN_FIND");
}