title: Spring-Security: MySQL JDBC Authentication fails
body: <p>I am manipulating an open source project in <a href="https://github.com/salman-/ignit-QA" rel="nofollow noreferrer">this repo</a>. The file <code>bank.sql</code> is the schema of the database in mysql. Here is the <code>pom.xml</code>:</p>
  
  <pre><code> &lt;dependencies&gt;
  
      &lt;!-- https://mvnrepository.com/artifact/org.apache.tomcat/juli --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
          &lt;artifactId&gt;juli&lt;/artifactId&gt;
          &lt;version&gt;6.0.26&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
          &lt;version&gt;2.0&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
  
       &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
          &lt;version&gt;2.5&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
    &lt;dependency&gt;
     &lt;groupId&gt;junit&lt;/groupId&gt;
     &lt;artifactId&gt;junit&lt;/artifactId&gt;
     &lt;version&gt;4.11&lt;/version&gt;
     &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
       &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
       &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
       &lt;groupId&gt;org.springframework&lt;/groupId&gt;
       &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
       &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
        &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
        &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
        &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
        &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
         &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
         &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
         &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
         &lt;groupId&gt;org.springframework&lt;/groupId&gt;
         &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
         &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
         &lt;groupId&gt;mysql&lt;/groupId&gt;
         &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
         &lt;version&gt;5.1.6&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
         &lt;groupId&gt;jstl&lt;/groupId&gt;
         &lt;artifactId&gt;jstl&lt;/artifactId&gt;
         &lt;version&gt;1.2&lt;/version&gt;
    &lt;/dependency&gt;
  
    &lt;dependency&gt;
         &lt;groupId&gt;opensymphony&lt;/groupId&gt;
         &lt;artifactId&gt;sitemesh&lt;/artifactId&gt;
         &lt;version&gt;2.4.2&lt;/version&gt;
    &lt;/dependency&gt;
   &lt;/dependencies&gt;
  </code></pre>
  
  <p>I have a login form as below:</p>
  
  <pre><code>    &lt;form name="loginForm" class="form-login"
          action="&lt;c:url value="/j_spring_security_check" /&gt;" method="POST"&gt;
          &lt;h2&gt;Please sign in&lt;/h2&gt;
  
          &lt;c:if test="${not empty error}"&gt;
              &lt;div class="alert alert-danger"&gt;${error}&lt;/div&gt;
          &lt;/c:if&gt;
          &lt;c:if test="${not empty msg}"&gt;
              &lt;div class="alert alert-info"&gt;${msg}&lt;/div&gt;
          &lt;/c:if&gt;
  
          &lt;input type="text" class="form-control" placeholder="Username" name="username"&gt;
          &lt;input type="password" class="form-control" placeholder="Password" name="password" /&gt;
          &lt;button type="submit" class="btn btn-lg btn-primary btn-block" name="submit"&gt;Login&lt;/button&gt;
          &lt;input type="hidden" name="${_csrf.parameterName}"
              value="${_csrf.token}" /&gt;
  
      &lt;/form&gt;
  </code></pre>
  
  <p>The file <code>Spring-Security.xml</code> is as below:</p>
  
  <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.2.xsd"&gt;
  
      &lt;http auto-config="true" use-expressions="true"&gt;
          &lt;intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" /&gt;
          &lt;intercept-url pattern="/user**" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" /&gt;
          &lt;intercept-url pattern="/change**" access="hasRole('ROLE_NEWUSER')" /&gt;
  
          &lt;access-denied-handler error-page="/403" /&gt;
  
          &lt;form-login 
              login-page="/login" 
              authentication-success-handler-ref="bankCustomAuthenticationSuccessHandler"
              authentication-failure-url="/login?error" 
              username-parameter="username"
              password-parameter="password" /&gt;
          &lt;logout logout-success-url="/login?logout"  /&gt;
          &lt;!-- enable csrf protection --&gt;
          &lt;csrf/&gt;
      &lt;/http&gt;
  
      &lt;beans:bean id="bankCustomAuthenticationSuccessHandler"
          class="ee.mikkelsaar.bank.security.MyUrlAuthenticationSuccessHandler" /&gt;
  
      &lt;authentication-manager&gt;
          &lt;authentication-provider&gt;
              &lt;password-encoder hash="sha" /&gt;
              &lt;jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select u.username, a.authority from users u, authorities a where u.username = a.username and u.username =?" /&gt;
          &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  
      &lt;beans:import resource="spring-datasource.xml" /&gt;
  
      &lt;beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder"&gt;
        &lt;beans:constructor-arg value="sha" /&gt;
      &lt;/beans:bean&gt;
  
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>And there is a bean to obtain the datasource to provide it for <code>Authentication-manager</code> such as below:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/bank" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I am sure the MySQL Server is running on port 3306 well.</p>
  
  <p>The correct credential is <code>username:Tom</code> and <code>password:Tom</code> but everytime I try to login with them it fails. I am wondering, what is wrong with my authentication process?</p>
  
  <p>How can i fix it?</p>
  
  <p>I guess, maybe the datasource bean is not created correctly, but i have no idea how to check it?</p>
  
  <p>Update:</p>
  
  <p>When i add the <code>&lt;http security="none" pattern="/login"/&gt;</code> to my <code>Spring-Security.xml</code> it complains with </p>
  
  <pre><code>HTTP Status 405 - Request method 'POST' not supported for (username, password) `(Tom, tom)`, which is not a valid credential. But for a valid credential like `(Tom,Tom)` is still navigates to the login page again. 
  </code></pre>
  
  <p>but it happens</p>