SEARCH BY

All

  • All
  • Java8
  • Spring

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

Post a Comment
Upto Spring 4 password can be stored as a plain text in inmemory process.

But from Spring 5 password must be encoded


from Spring 5 we have to use {noop}

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication()
   .withUser("user").password("{noop}password").roles("USER");
 }
}

or we can use NoOpPasswordEncoder instance, we can pass NoOpPasswordEncoder instance to passwordEncoder() method calling on inMemoryAuthentication().

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication()
   .passwordEncoder(NoOpPasswordEncoder.getInstance())
   .withUser("user").password("password").roles("USER");
 }
}

Alternatively we can user PasswordEncoderFactories to create encoded password

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

  auth.inMemoryAuthentication()
   .withUser("user")
   .password(encoder.encode("password"))
   .roles("USER");
 }
}



jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox