title: implemention of login using UserDetails in Spring Security does not work
body: <p>I have a simple Spring Boot 2.xx App with Spring Data JPA MySQL, I have to secure this web app using Spring Security (especially Java config) in this regard I found plenty of resources online to get the Job done but <strong>in vain</strong>, some examples were too complex to understand and other examples never worked my on side. I came up with this <a href="https://github.com/arshadalisoomro/secure-spring-demo" rel="nofollow noreferrer">secure-spring-demo</a>, which is also not working. some code snips are</p>
  
  <p>WebSecurityConfig class is</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserDetailsServiceImpl userDetailsService;
  
      @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Bean
      public DaoAuthenticationProvider authProvider() {
          DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
          authProvider.setUserDetailsService(userDetailsService);
          authProvider.setPasswordEncoder(passwordEncoder());
          return authProvider;
      }
  
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(authProvider());
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable();
          http
                  .authorizeRequests()
                  .antMatchers("/login*", "/register", "/").anonymous()
                  .antMatchers("/secure/*").hasAnyAuthority("ADMIN").anyRequest().authenticated()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .loginPage("/login")
                  .and()
                  .logout().logoutSuccessUrl("/login");
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  }
  </code></pre>
  
  <p>and UserDetailsServiceImpl class is </p>
  
  <pre><code>@Service
  public class UserDetailsServiceImpl implements UserDetailsService {
  
      private final UserService userService;
  
      @Autowired
      public UserDetailsServiceImpl(UserService userService) {
          this.userService = userService;
      }
  
      @Override
      public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
          User user = userService.findByUserEmail(s);
          if (user == null){
              throw new UsernameNotFoundException("User with " + s + " not found!");
          }
  
          return new org.springframework.security.core.userdetails.User(
                  user.getUserEmail(), user.getUserPassword(), user.getRoles());
      }
  }
  </code></pre>
  
  <p>the controller is </p>
  
  <pre><code>@Controller
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @Autowired
      private RoleService roleService;
  
      @Autowired
      private SecurityService securityService;
  
      //Omitting other mappings
  
      @GetMapping(value = "/login")
      public String getLogin(Model model,
                             @RequestParam(value = "error", required = false) String error){
          if (null != error &amp;&amp; error.equalsIgnoreCase("true")){
              model.addAttribute("loginError", "Unable to Login");
          }
          return "login";
      }
  
      @PostMapping(value = "/login")
      public String postLogin(@RequestParam(value = "userEmail") String userEmail,
                                @RequestParam(value = "userPassword") String userPassword){
          logger.debug(userEmail + " and " + userPassword );
          boolean loginResult = securityService.login(userEmail, userPassword);
          return (loginResult ? "redirect:/secure/home" : "redirect:/login?error=true");
      }
  
  }
  </code></pre>
  
  <p>with this all the user is not able to login with correct credentials. </p>