title: Spring Security BadCredentialsException
body: <p>I am following <a href="https://www.baeldung.com/spring-security-registration-password-encoding-bcrypt" rel="nofollow noreferrer">this Baeldung tutorial</a>, and I can't see any differences (except maybe the pregenerated login page template), but I still get a <code>BadCredentialsException</code> when trying to login with a hard-coded user/password combo, which I've already confirmed is in the DB and the password in there is encrypted.</p>
  
  <p>Here's my code, let me know if more is needed:</p>
  
  <p>SecurityConfig:</p>
  
  <pre><code>@Component
  @EnableWebSecurity
  @SuppressWarnings("unused")
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private MyUserDetailsService userDetailsService;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests()
                  .antMatchers("/css/**").permitAll()
                  .antMatchers("/error").permitAll()
                  .antMatchers("/action/**").hasRole("USER")
                  .antMatchers("/action_template/**").hasRole("ADMIN")
                  .and()
                  .formLogin()
                  .loginPage("/login").failureUrl("/login-error");
      }
  
      @Override
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(authProvider());
      }
  
      @Bean
      public DaoAuthenticationProvider authProvider() {
          DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  
          authProvider.setUserDetailsService(userDetailsService);
          authProvider.setPasswordEncoder(passwordEncoder());
  
          return authProvider;
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
  }
  </code></pre>
  
  <p>MyUserDetailsService:</p>
  
  <pre><code>@Service
  @Transactional
  @SuppressWarnings("unused")
  public class MyUserDetailsService implements UserDetailsService {
  
      @Autowired
      private UserRepository userRepository;
  
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
  
          User user = userRepository.findByEmail(email);
  
          if (user == null) {
              throw new UsernameNotFoundException(
                      "No user found with username: " + email);
          }
  
          final List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;&gt;();
          user.getRoles().forEach((r) -&gt; authorities.add(new SimpleGrantedAuthority(r)));
  
          return  new org.springframework.security.core.userdetails.User(
                  user.getEmail(),
                  user.getPassword().toLowerCase(),
                  true,
                  true,
                  true,
                  true,
                  authorities
          );
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's the stacktrace</p>
  
  <pre><code>2018-11-14 13:52:05.785 DEBUG 2100 --- [nio-8090-exec-5] w.a.UsernamePasswordAuthenticationFilter : Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials
  
  org.springframework.security.authentication.BadCredentialsException: Bad credentials
      at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:93)
      at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
      at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199)
      at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>I've also edited my SecurityConfig and added <code>.antMatchers("/error").permitAll()</code>, but it didn't help</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Here's the <a href="https://github.com/VicenteRD/TrackerDNA" rel="nofollow noreferrer">Github repo</a>, it should provide more insight.
  I have no clue on how to diagnose this further...</p>