Spring Security There is no PasswordEncoder mapped for the id null
By:Roy.LiuLast updated:2019-08-11
Send a GET request with username and password, but hits the password encoder error?
Tested
- Spring Boot 2.1.2.RELEASE
- 5.1.3.RELEASE
$ curl localhost:8080/books -u user:password "timestamp":"2019-02-22T15:03:49.322+0000", "status":500, "error":"Internal Server Error", "message":"There is no PasswordEncoder mapped for the id \"null\"", "path":"/books"
errors in logs
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
Here is the configuration.
SpringSecurityConfig.java
package com.mkyong.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("password").roles("USER") .and() .withUser("admin").password("password").roles("ADMIN");
Solution
Prior to Spring Security 5.0 the default PasswordEncoder was NoOpPasswordEncoder which required plain text passwords. In Spring Security 5, the default is DelegatingPasswordEncoder, which required Password Storage Format.
Solution 1 – Add password storage format, for plain text, add {noop}
SpringSecurityConfig.java
package com.mkyong.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("ADMIN");
Solution 2 – User.withDefaultPasswordEncoder() for UserDetailsService
SpringSecurityConfig.java
package com.mkyong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public UserDetailsService userDetailsService() { User.UserBuilder users = User.withDefaultPasswordEncoder(); InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); manager.createUser(users.username("user").password("password").roles("USER").build()); manager.createUser(users.username("admin").password("password").roles("USER", "ADMIN").build()); return manager;
From:一号门
COMMENTS