title: Spring Security 5 : There is no PasswordEncoder mapped for the id "null"
body: <p>I am migrating from Spring Boot 1.4.9 to Spring Boot 2.0 and also to Spring Security 5 and I am trying to do authenticate via OAuth 2. But I am getting this error: </p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null </p>
  </blockquote>
  
  <p>From the documentation of <a href="https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding" rel="noreferrer">Spring Security 5</a>, I get to know that
  storage format for password is changed.</p>
  
  <p>In my current code I have created my password encoder bean as:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Bean
  public BCryptPasswordEncoder passwordEncoder() {
      return new BCryptPasswordEncoder();
  }
  </code></pre>
  
  <p>However it was giving me below error: </p>
  
  <blockquote>
    <p><strong>Encoded password does not look like BCrypt</strong></p>
  </blockquote>
  
  <p>So I update the encoder as per the <a href="https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding" rel="noreferrer">Spring Security 5</a> document to:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Bean
  public PasswordEncoder passwordEncoder() {
      return PasswordEncoderFactories.createDelegatingPasswordEncoder();
  }
  </code></pre>
  
  <p>Now if I can see password in database it is storing as </p>
  
  <pre><code>{bcrypt}$2a$10$LoV/3z36G86x6Gn101aekuz3q9d7yfBp3jFn7dzNN/AL5630FyUQ
  </code></pre>
  
  <p>With that 1st error gone and now when I am trying to do authentication I am getting below error:</p>
  
  <blockquote>
    <p>java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null</p>
  </blockquote>
  
  <p>To solve this issue I tried all the below questions from Stackoverflow:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/46999940/spring-boot-passwordencoder-error?noredirect=1&amp;lq=1">Spring Boot PasswordEncoder Error</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/26013251/spring-oauth2-password-encoder-is-not-set-in-daoauthenticationprovider">Spring Oauth2. Password encoder is not set in DaoAuthenticationProvider</a> </p></li>
  </ul>
  
  <p>Here is a question similar to mine but not answerd:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/49236707/spring-security-5-password-migration">Spring Security 5 - Password Migration</a></li>
  </ul>
  
  <p>NOTE: I am already storing encrypted password in database so no need to encode again in <code>UserDetailsService</code>.</p>
  
  <p>In the <a href="https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding" rel="noreferrer">Spring security 5</a> documentation they suggested you can handle this exception using:</p>
  
  <blockquote>
    <p>DelegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(PasswordEncoder)</p>
  </blockquote>
  
  <p>If this is the fix then where should I put it? I have tried to put it in <code>PasswordEncoder</code> bean like below but it wasn't working: </p>
  
  
  
  <pre><code>DelegatingPasswordEncoder def = new DelegatingPasswordEncoder(idForEncode, encoders);
  def.setDefaultPasswordEncoderForMatches(passwordEncoder);
  </code></pre>
  
  <p><strong>MyWebSecurity class</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return PasswordEncoderFactories.createDelegatingPasswordEncoder();
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
  
          web
                  .ignoring()
                  .antMatchers(HttpMethod.OPTIONS)
                  .antMatchers("/api/user/add");
      }
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  }
  </code></pre>
  
  <p><strong>MyOauth2 Configuration</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableAuthorizationServer
  protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  
      @Bean
      public TokenStore tokenStore() {
          return new InMemoryTokenStore();
      }
  
      @Autowired
      @Qualifier("authenticationManagerBean")
      private AuthenticationManager authenticationManager;
  
  
      @Bean
      public TokenEnhancer tokenEnhancer() {
          return new CustomTokenEnhancer();
      }
  
      @Bean
      public DefaultAccessTokenConverter accessTokenConverter() {
          return new DefaultAccessTokenConverter();
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints)
              throws Exception {
          endpoints
                  .tokenStore(tokenStore())
                  .tokenEnhancer(tokenEnhancer())
                  .accessTokenConverter(accessTokenConverter())
                  .authenticationManager(authenticationManager);
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients
                  .inMemory()
                  .withClient("test")
                  .scopes("read", "write")
                  .authorities(Roles.ADMIN.name(), Roles.USER.name())
                  .authorizedGrantTypes("password", "refresh_token")
                  .secret("secret")
                  .accessTokenValiditySeconds(1800);
      }
  }
  </code></pre>
  
  <p>Please guide me with this issue. I have spend hours to fix this but not able to fix.</p>