title: Spring Boot auto login Bad Credentials
body: <p>Ive been at this for hours and still cant figure out why im getting <code>org.springframework.security.authentication.BadCredentialsException</code></p>
  <p>Im working on a registration and login system.  I can create a User through a registration page and I can see it inputted into the database.  In the registration post method I then try to automatically login the user with an authentication manager but each time I get the same BadCredentialsException.  I check in my database and everything looks fine yet I still get the same error.  Im new to the authentication stuff so my mistake may be in plain site to others but not myself.</p>
  <pre><code>UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword());
          request.getSession();
  
          token.setDetails(new WebAuthenticationDetails(request));
  
          try{
              Authentication auth = authManager.authenticate(token);
  
              SecurityContextHolder.getContext().setAuthentication(auth);
          } catch(Exception e){
                  e.printStackTrace();
          }
  </code></pre>
  <p>That is where I try to log in the User, with that code in which should Auto login the User I get the bad credentials exception.  I do save the User correctly before that.  I tried many different ways to auto login the user but none of them have worked.  Any ideas?</p>
  <p>Edit:
  Security configuration:</p>
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Autowired
  private UserService userService;
  
  @Autowired
  UserDetailsService userDetailsService;
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http.cors().and().csrf().disable().authorizeRequests()
      .antMatchers(&quot;/register&quot;).permitAll()
      .antMatchers(&quot;/edit&quot;).hasRole(&quot;ADMIN&quot;)
      .antMatchers(
      (A bunch of allowances to pages), &quot;/&quot;).permitAll()
      .anyRequest().authenticated()
      .and()
      .formLogin()
      .loginPage(&quot;/login&quot;)
      .permitAll()
      .and()
      .rememberMe()
      .key(&quot;myUniqueKey&quot;) 
      .userDetailsService(userDetailsService)
      .rememberMeCookieName(&quot;remember-me&quot;)
      .tokenValiditySeconds(100000000)
      .and()
      .logout()
      .invalidateHttpSession(true)
      .clearAuthentication(true)
      .logoutRequestMatcher(new AntPathRequestMatcher(&quot;/logout&quot;))
      .logoutSuccessUrl(&quot;/login?logout&quot;)
      .permitAll();
  }
  
  @Bean
  public BCryptPasswordEncoder passwordEncoder() {
      return new BCryptPasswordEncoder();
  }
  
  @Bean
  public DaoAuthenticationProvider authenticationProvider() {
      DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
      auth.setUserDetailsService(userService);
      auth.setPasswordEncoder(passwordEncoder());
      return auth;
  }
  
  @Bean
  public AuthenticationManager customAuthenticationManager() throw Exception {
      return authenticationManager();
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(authenticationProvider());
  }
  </code></pre>