title: Spring Security:DataBase authentication provider
body: <p>Can't get <code>Spring Security</code> to work with DB authentication provider.<br>
  In-memory authentication provider works OK.</p>
  
  <p><strong>Step to reproduce:</strong><br>
  when I logged with credentials <code>sb</code>,<code>sb</code>,<code>login()</code> method of <code>AuthenticationService</code> returned <code>false</code>.<br>
  There are no related log in <code>Tomcat</code>.</p>
  
  <p><strong>applicationContext.xml:</strong></p>
  
  <pre><code>&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/chirokDB?useUnicode=true&amp;amp;characterEncoding=utf8"/&gt;
      &lt;property name="username" value="root"/&gt;
      &lt;property name="password" value="root"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>service layer:</strong></p>
  
  <pre><code>@Service("authenticationService")
     public class AuthenticationServiceImpl implements AuthenticationService {
      @Resource(name = "authenticationManager")
      private AuthenticationManager authenticationManager;
          public boolean login(String username, String password) {
          try {
          Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                      username, password));
              if (authenticate.isAuthenticated()) {
      SecurityContextHolder.getContext().setAuthentication(authenticate);
                      return true;
                  }
              } catch (AuthenticationException e) {
              }
              return false;
     }
  </code></pre>
  
  <p><strong>managed bean level:</strong></p>
  
  <pre><code>public String doLogin() {
      boolean isLoggedIn = authenticationService.login(name, password);
      if (isLoggedIn) {
          return "index";
      }
      FacesContext.getCurrentInstance().addMessage("login failure", new FacesMessage());
      return "failureLogin";
  }
  </code></pre>
  
  <p><strong>applicationContext-security.xml:</strong></p>
  
  <pre><code>&lt;global-method-security pre-post-annotations="enabled"/&gt;  
      &lt;http auto-config="true"&gt;
      &lt;form-login login-page="/login.xhtml" default-target-url="/index.xhtml"/&gt;
          &lt;intercept-url pattern="/contacts.xhtml" access="ROLE_ANONYMOUS,ROLE_USER"/&gt;
          &lt;intercept-url pattern="/delivery.xhtml" access="ROLE_USER"/&gt;
          &lt;logout invalidate-session="true"/&gt;
          &lt;session-management&gt;
              &lt;concurrency-control max-sessions="1" error-if-maximum-exceeded="true"/&gt;
          &lt;/session-management&gt;   
      &lt;/http&gt;          
  
      &lt;authentication-manager alias="authenticationManager"&gt;
          &lt;authentication-provider&gt;
              &lt;jdbc-user-service data-source-ref="dataSource"/&gt;
          &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  </code></pre>
  
  <p><strong>persistence level:</strong><br>
  <code>MySql DB</code> has following standard tables(required by Spring):<br>
  1. users<br>
  2. authorities</p>
  
  <p><code>users</code> table has record with username='sb' and password='sb'<br>
  <code>authorities</code> table has record with username='sb' and authority='ROLE_USER'</p>
  
  <p><strong>note</strong><br>
  with user-in memory all works OK with following config:</p>
  
  <pre><code>    &lt;authentication-manager alias="authenticationManager"&gt;
          &lt;authentication-provider&gt;
              &lt;user-service&gt;
                  &lt;user name="sb" password="sb" authorities="ROLE_USER"/&gt;
              &lt;/user-service&gt;
          &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  </code></pre>
  
  <p><strong>assumption:</strong><br>
  <code>dataSource</code> injected into <code>org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl</code><br>
  As far Hibernate ORM used, perhaps some other than <code>JdbcDaoImpl</code> should be used?</p>