title: Spring Security 4 : Authentication failed: password does not match stored value
body: <p>I'm using spring security in my application. User can correctly login to system, but after few logins and logouts it is no more possible to login again. I'm getting an error: </p>
  
  <pre><code>Authentication failed: password does not match stored value
  </code></pre>
  
  <p>I'm using java config like this: </p>
  
  <pre><code>import javax.sql.DataSource;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.ImportResource;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  
  @Configuration
  @ImportResource("classpath:/root-context.xml")
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private DataSource dataSource;
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder())
              .usersByUsernameQuery("select email,password, enabled from users where email=?")
              .authoritiesByUsernameQuery("select email, role from user_roles where email=?");
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.exceptionHandling().accessDeniedPage("/403").and().authorizeRequests().antMatchers("/resources/**")
              .permitAll().anyRequest().hasAnyRole("EMPLOYEE", "ADMIN").and().formLogin().loginPage("/login")
              .permitAll().and().logout().logoutSuccessUrl("/logout").permitAll();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone tell me, what could be a problem? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found out, that for some reason, the saved hash in DB is no more correct, if I replace it in DB with another one, I can again log in. What is causing it? </p>