title: Springboot Security: Passwords do not match during login
body: <p>I am using BCryptPasswordEncoder in order to encrypt a users registration and login. </p>
  
  <p>The registration part works fine, it puts a new user into the database with a password such as:</p>
  
  <pre><code> '$2a$10$aUk/26idLhSaNmhNRTRejd03FnxxLxv6X0Uo0P4PcA4mbyy.
  </code></pre>
  
  <p>When I come to login, the username entered matches and I successfully find a user from the repository.</p>
  
  <p>I am then told that the username or password is wrong. When I remove this encryption away from the program it works fine. So essentially I am doing something wrong when comparing the encrypted passwords.</p>
  
  <p>Here is my UserDetailsService implementation logic:</p>
  
  <pre><code> public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException {
  
          System.out.println(username);
  
          User user = userRepository.findByUsername(username);
          System.out.println(user.getPassword());
  
          if (user.getUsername().isEmpty()) {
              throw new UsernameNotFoundException(
                      "No user found with username: "+ username);
          }
          boolean enabled = true;
          boolean accountNonExpired = true;
          boolean credentialsNonExpired = true;
          boolean accountNonLocked = true;
          return  new org.springframework.security.core.userdetails.User
                  (user.getUsername(),
                          user.getPassword().toLowerCase(), enabled, accountNonExpired,
                          credentialsNonExpired, accountNonLocked,
                          getAuthorities(Arrays.asList("ROLE_USER")));
      }
  
      private static List&lt;GrantedAuthority&gt; getAuthorities (List&lt;String&gt; roles) {
          List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;&gt;();
          for (String role : roles) {
              authorities.add(new SimpleGrantedAuthority(role));
          }
          return authorities;
  </code></pre>
  
  <p>I also set up the beans in the web security file:</p>
  
  <pre><code>  @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
      }
  
  
  @Bean
  public PasswordEncoder passwordEncoder(){
      PasswordEncoder encoder = new BCryptPasswordEncoder();
      return encoder;
  }
  </code></pre>
  
  <p>What am i doing wrong? Thanks.</p>