title: Spring 4 security login users-by-username-query is always login error
body: <p>security-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
      xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
  
  
      &lt;security:authentication-manager&gt;
          &lt;security:authentication-provider&gt;
              &lt;security:jdbc-user-service
                  data-source-ref="dataSource"
                  users-by-username-query="select username, password, enabled from users where username=?"
                  authorities-by-username-query="select username, authority  from authorities where username=?" /&gt;
              &lt;security:password-encoder ref="passwordEncoder"&gt;&lt;/security:password-encoder&gt;
  
          &lt;/security:authentication-provider&gt;
  
      &lt;/security:authentication-manager&gt;
      &lt;security:http auto-config="true" create-session="always"
          use-expressions="true"&gt;
  
          &lt;security:csrf disabled="true" /&gt;
  
  
          &lt;security:intercept-url pattern="/" access="permitAll" /&gt;
          &lt;security:intercept-url pattern="/home" access="permitAll" /&gt;
          &lt;security:intercept-url pattern="/admin/**"
              access='hasRole("ROLE_ADMIN")' /&gt;
  
          &lt;security:form-login login-page="/login"
              authentication-failure-url="/login?error=1" default-target-url="/" /&gt;
  
  
          &lt;security:headers disabled="true"&gt;&lt;/security:headers&gt;
      &lt;/security:http&gt;
      &lt;bean
          class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"
          id="passwordEncoder"&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>login Controller</p>
  
  <pre><code>package my.custom.project.controller;
  
  import java.util.List;
  import java.util.Locale;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  
  import my.custom.project.model.User;
  import my.custom.project.service.UserService;
  
  @Controller
  public class LoginController {
  
  
      @Autowired
      private UserService userService;
  
      @RequestMapping("/login")
      public String Login(@RequestParam(value="error", required=false) String error, 
              @RequestParam(value="logout", required=false) String logout, Model model){
  
          if (error!=null){
              model.addAttribute("errorMsg","Invalid username and password");
          }
          if(logout!=null){
              model.addAttribute("logoutMsg", "You have been logged out successfully");
          }
  
  
          return "login";
      }
  
  }
  </code></pre>
  
  <p>login.jsp</p>
  
  <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  
  &lt;main role="main" style="margin-top:30px;" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4"&gt;
  &lt;div class="container-wrapper"&gt;
      &lt;div class="container"&gt;
          &lt;h2&gt;Login with username and password&lt;/h2&gt;
          &lt;c:if test="${not empty errorMsg}"&gt;
              &lt;div style="color: #ff0000"&gt;
                  &lt;h3&gt;${errorMsg}&lt;/h3&gt;
              &lt;/div&gt;
          &lt;/c:if&gt;
          &lt;c:if test="${not empty logoutMsg}"&gt;
              &lt;div style="color: #0000ff"&gt;
                  &lt;h3&gt;${logoutMsg}&lt;/h3&gt;
              &lt;/div&gt;
          &lt;/c:if&gt;
  
  
          &lt;form action="&lt;c:url value="/login"/&gt;" method="post"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="username"&gt;Username:&lt;/label&gt; &lt;input type="text"
                      class="form-control" id="uesrname" placeholder="Enter username"
                      name="username" style="width: 50%"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label for="pwd"&gt;Password:&lt;/label&gt; &lt;input type="password"
                      class="form-control" id="passwd" placeholder="Enter password"
                      name="password" style="width: 50%"&gt;
              &lt;/div&gt;
  
              &lt;input type="hidden" name="${_csrf.parameterName}"
                  value="${_csrf.token}" /&gt;
  
  
              &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/main&gt;
  </code></pre>
  
  <p>MySQL DB</p>
  
  <p><a href="https://i.stack.imgur.com/pkzxY.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>And Result is...
  <a href="https://i.stack.imgur.com/S3eII.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>username, passworr is correct. but result is always failed.
  I can receive users's data in controller by userService.
  I guess useDao is correct working.
  Is problem at spring security security:jdbc-user-service ?</p>
  
  <p>How to resolve this problem?</p>