title: How to programmatically authenticate `User` with spring security and use my `UserDetailsServie` implementation?
body: <p>I want to use spring security <code>UserDetailsService</code> together with spring data jpa <code>CrudRepository</code> interface.
  Principal is represented by <code>User</code> entity which implements <code>UserDetails</code>:</p>
  
  <pre><code>@Entity
  public class User implements UserDetails {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
  
      private String username;
      private String password;
  
      //other methods of UserDetails 
  </code></pre>
  
  <p>I have spring-security.xml config file:</p>
  
  <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-4.0.xsd"&gt;
  
      &lt;authentication-manager&gt;
          &lt;!-- refers to spring data repository bean org.baeldung.SpringDataAuditDemo.dao.repos.UserDao --&gt;
          &lt;authentication-provider user-service-ref="userDao" /&gt;
      &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>It refers to bean <code>userDao</code> of type <code>UserDetailsService</code> and <code>CrudRepository</code>:</p>
  
  <pre><code>public interface UserDao extends CrudRepository&lt;User, Long&gt;, UserDetailsService {
  
      // @Override    
      @Query(value = "select u from User u where u.username=:username")
      public UserDetails loadUserByUsername(@Param("username") String username) throws UsernameNotFoundException;
  }
  </code></pre>
  
  <p>Now I want to authenticate <code>User</code> object programmatically in jUnit tests.</p>
  
  <p>I hope to run <code>UserDao.loadUserByUsername</code> method when authenticating <code>User</code>.
  For that I wanted to  use <code>org.springframework.security.provisioning.UserDetailsManager</code> but it implements <code>UserDetailsService</code> interface as well as my <code>UserDao</code>. Therefore I got exception that two beans of same type exists!!!</p>
  
  <p>Then I tried to use <code>UserDao</code> instead of <code>UserDetailsManager</code> but my <code>UserDao</code> doesn't authenticate <code>User</code>, but only loads it from db. In this case <code>spring-security.xml</code> is not needed.</p>
  
  <p>How to programmatically authenticate <code>User</code> with spring security default <code>DaoAuthenticationProvider</code> and use my <code>UserDetailsServie</code> implementation?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When I tried to autowire my <code>UserDao</code> with <code>@Qualifier</code> and <code>UserDetailsManager</code> without it, I got this exception :</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.provisioning.UserDetailsManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
  ... 28 more
  </code></pre>