title: Cannot login with Spring Boot Security
body: <p>I have a Spring Boot app with MySQL. I can save Users in the database but when I try to login, it keeps saying that the username or password is incorrect. Even when I hard code an User, it still doesn't work. </p>
  
  <p>I created the setup using the <a href="https://www.youtube.com/watch?v=TNt3GHuayXs" rel="nofollow noreferrer">JavaBrains tutorial</a> and their code on <a href="https://github.com/koushikkothagal/spring-security-jpa" rel="nofollow noreferrer">GitHub</a>.</p>
  
  <p>This is the code I'm using.</p>
  
  <p>Security config:</p>
  
  <pre><code>@EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      UserDetailsService userDetailsService;
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .authorizeRequests(authorize -&gt; authorize
                          .antMatchers("/static/**", "/index").permitAll()
                          .antMatchers("/premium/**").hasAuthority("PREMIUM")
                          .antMatchers("/user/**").hasAuthority("USER")
                  )
                  .formLogin(formLogin -&gt; formLogin
                          .loginPage("/login")
                          .failureUrl("/login-error")
                  );
      }
  
      @Bean
      public PasswordEncoder getPasswordEncoder() {
          return NoOpPasswordEncoder.getInstance();
      }
  }
  </code></pre>
  
  <p>My UserDetailsService</p>
  
  <pre><code>public class MyUserDetailsService implements UserDetailsService {
  
      @Autowired
      UserRepository userRepository;
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          Optional&lt;User&gt; user = userRepository.findByUsername(username);
          user.orElseThrow(() -&gt; new UsernameNotFoundException("Can't find user " + username));
          return user.map(MyUserDetails::new).get();
      }
  }
  </code></pre>
  
  <p>My UserDetails</p>
  
  <pre><code>public class MyUserDetails implements UserDetails {
  
      private String username;
      private String password;
      private boolean isEnabled;
      private List&lt;GrantedAuthority&gt; authorities;
  
      public MyUserDetails(User user) {
          this.username = user.getUsername();
          this.password = user.getPassword();
          this.isEnabled = user.isEnabled();
          this.authorities = Arrays.stream(user.getAuthorities().split(","))
                  .map(SimpleGrantedAuthority::new).collect(Collectors.toList());
      }
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          return authorities;
      }
  
      @Override
      public String getPassword() {
          return password;
      }
  
      @Override
      public String getUsername() {
          return username;
      }
  
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
  
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
  
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
  
      @Override
      public boolean isEnabled() {
          return isEnabled;
      }
  }
  </code></pre>
  
  <p>My UserRepository</p>
  
  <pre><code>@Repository
  public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  
      Optional&lt;User&gt; findByUsername(String username);
  
  }
  </code></pre>
  
  <p>User</p>
  
  <pre><code>@Entity(name="Users")
  public class User {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
      private String username;
      private String password;
  
      @Transient
      private String passwordConfirm;
      private boolean isEnabled;
      private String authorities;
      private String isPremium;
      private LocalDateTime premiumExpiryDate;
  
      // getters and setters
  }
  </code></pre>
  
  <p>Login form on the login page</p>
  
  <pre><code>&lt;p th:if="${loginError}" class="error"&gt;Wrong user or password&lt;/p&gt;
  &lt;form th:action="@{/login}" method="post" class="login100-form validate-form p-l-55 p-r-55 p-t-178"&gt;
          &lt;span class="login100-form-title"&gt;
              Sign In
          &lt;/span&gt;
  
      &lt;div class="wrap-input100 validate-input m-b-16" data-validate="Please enter username"&gt;
          &lt;input class="input100" type="text" name="username" placeholder="Username"&gt;
          &lt;span class="focus-input100"&gt;&lt;/span&gt;
      &lt;/div&gt;
  
      &lt;div class="wrap-input100 validate-input" data-validate = "Please enter password"&gt;
          &lt;input class="input100" type="password" name="password" placeholder="Password"&gt;
          &lt;span class="focus-input100"&gt;&lt;/span&gt;
      &lt;/div&gt;
  
      &lt;div class="text-right p-t-13 p-b-23"&gt;
              &lt;span class="txt1"&gt;
                  Forgot
              &lt;/span&gt;
  
          &lt;a href="#" class="txt2"&gt;
              Username / Password?
          &lt;/a&gt;
      &lt;/div&gt;
  
      &lt;div class="container-login100-form-btn"&gt;
          &lt;button class="login100-form-btn"&gt;
              Sign in
          &lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I don't have a POST mapping for the login as it is supposed to be provided by Spring security. However I suspect the problem must be somewhere there, because I had log statements in the UserDetails and in the UserDetailsService but none of those got invoked.</p>
  
  <p>Can you help me figure out what is the problem here? </p>