[Fixed] Spring Security Error: There is no PasswordEncoder mapped for the id "null"
- Details
- Written by Nam Ha Minh
- Last Updated on 31 March 2020   |   Print Email
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
Or null can be any unknown password encoding algorithm.To fix this error, you need to specify a password encoder explicitly this way:public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { ... } @Override protected void configure(HttpSecurity http) throws Exception { ... } }Or, in case you’re using in-memory authentication, specify the password encoder like this:
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(new BCryptPasswordEncoder()) .withUser("namhm").password("$2a$10$fUXt47JTx/Rv/OHBkQgqAOvan445zDU7tCZcHr...") .roles("USER") ; }If you are using JDBC, fix the error “there is no PasswordEncoder mapped” as follows:
@Autowired public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().passwordEncoder(new BCryptPasswordEncoder()); }You may also get the same error if you specify wrong prefix for in-memory password accidentally like this:
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("namhm").password("{bvrypt}$2a$10$fUXt47JTx/Rv/OHBkQgqAOvan445zDU7tCZcHr...") .roles("USER") ; }Spring will report this error:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "bvrypt"
Related Spring Security Tutorials:
- Spring Web MVC Security Basic Example Part 1 with XML Configuration
- Spring Web MVC Security Basic Example Part 2 (Java-based Configuration)
Other Spring Tutorials:
- Understand the core of Spring framework
- Understand Spring MVC
- Understand Spring AOP
- Spring MVC beginner tutorial with Spring Tool Suite IDE
- Spring MVC Form Handling Tutorial
- Spring MVC Form Validation Tutorial
- 14 Tips for Writing Spring MVC Controller
- Spring Web MVC Security Basic Example (XML Configuration)
- Understand Spring Data JPA with Simple Example
Comments