title: Spring Security Oauth - Custom UserDetailsService is never getting called
body: <p><strong>spring-security.xml</strong></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:oauth="http://www.springframework.org/schema/security/oauth2"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
  http://www.springframework.org/schema/security/oauth2   http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd "&gt;
  
    &lt;!-- Default url to get a token from OAuth --&gt;
    &lt;http pattern="/api/oauth/token" create-session="stateless" authentication-manager-ref="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
  
      &lt;csrf disabled="true"/&gt;
      &lt;intercept-url pattern="/api/oauth/token" access="isFullyAuthenticated()"/&gt;
      &lt;anonymous enabled="false"/&gt;
      &lt;http-basic entry-point-ref="clientAuthenticationEntryPoint"/&gt;
  
      &lt;custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER"/&gt;
      &lt;access-denied-handler ref="oauthAccessDeniedHandler"/&gt;
    &lt;/http&gt;
  
    &lt;!-- Protected sources  --&gt;
    &lt;http pattern="/api/services/**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security" use-expressions="true"&gt;
      &lt;anonymous enabled="false"/&gt;
      &lt;intercept-url pattern="/api/**" access="hasRole('ROLE_APP')"/&gt;
      &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/&gt;
      &lt;access-denied-handler ref="oauthAccessDeniedHandler"/&gt;
    &lt;/http&gt;
  
    &lt;bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"&gt;
      &lt;property name="authenticationManager" ref="clientAuthenticationManager"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans"&gt;
      &lt;constructor-arg&gt;
        &lt;list&gt;
          &lt;bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter"/&gt;
          &lt;bean class="org.springframework.security.access.vote.RoleVoter"/&gt;
          &lt;bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/&gt;
        &lt;/list&gt;
      &lt;/constructor-arg&gt;
    &lt;/bean&gt;
  
    &lt;authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;authentication-provider user-service-ref="clientDetailsUserService"/&gt;
    &lt;/authentication-manager&gt;
  
    &lt;sec:authentication-manager&gt;
      &lt;sec:authentication-provider user-service-ref='userDetailsServiceImpl'/&gt;
    &lt;/sec:authentication-manager&gt;
  
    &lt;bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"&gt;
      &lt;constructor-arg ref="clientDetails"/&gt;
    &lt;/bean&gt;
  
    &lt;authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;authentication-provider user-service-ref="userDetailsServiceImpl"&gt;&lt;/authentication-provider&gt;
    &lt;/authentication-manager&gt;
  
    &lt;!-- Token store --&gt;
    &lt;bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore"&gt;
      &lt;constructor-arg ref="dataSource"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"&gt;
      &lt;property name="tokenStore" ref="tokenStore"/&gt;
      &lt;property name="supportRefreshToken" value="true"/&gt;
      &lt;property name="accessTokenValiditySeconds" value="120"/&gt;
      &lt;property name="clientDetailsService" ref="clientDetails"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="oAuth2RequestFactory" class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory"&gt;
      &lt;constructor-arg ref="clientDetails"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="userApprovalHandler" class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler"&gt;
      &lt;property name="tokenStore" ref="tokenStore"/&gt;
      &lt;property name="requestFactory" ref="oAuth2RequestFactory"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="test/client"/&gt;
      &lt;property name="typeName" value="Basic"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="test/client"/&gt;
      &lt;property name="typeName" value="Basic"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler"/&gt;
  
    &lt;oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices" user-approval-handler-ref="userApprovalHandler"&gt;
      &lt;oauth:authorization-code/&gt;
      &lt;oauth:implicit/&gt;
      &lt;oauth:refresh-token/&gt;
      &lt;oauth:client-credentials/&gt;
      &lt;oauth:password/&gt;
    &lt;/oauth:authorization-server&gt;
  
    &lt;oauth:resource-server id="resourceServerFilter" resource-id="test" token-services-ref="tokenServices"/&gt;
  
    &lt;bean id="clientDetails" class="com.xxxxxx.service.ClientService"&gt;&lt;/bean&gt;
  
    &lt;sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"&gt;
      &lt;sec:expression-handler ref="oauthExpressionHandler"/&gt;
    &lt;/sec:global-method-security&gt;
  
    &lt;oauth:expression-handler id="oauthExpressionHandler"/&gt;
    &lt;oauth:web-expression-handler id="oauthWebExpressionHandler"/&gt;
  &lt;/beans&gt; 
  </code></pre>
  
  <p><strong>UserServiceImpl</strong></p>
  
  <pre><code>@Service
  public class UserDetailsServiceImpl implements UserDetailsService {
  
      @Autowired
      private OauthRepository oauthRepository;
  
      @Override
      public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
          User user = oauthRepository.getByUsername(s);
          return user;
      }
  
  }
  </code></pre>
  
  <p><strong>ClientServiceImpl</strong></p>
  
  <pre><code>@Component
  public class ClientService implements ClientDetailsService {
  
      @Autowired
      private OauthRepository oauthRepository;
  
      @Override
      public ClientDetails loadClientByClientId(String s) throws ClientRegistrationException {
          BaseClientDetails clientDetails = oauthRepository.getByClientId(s);
          return clientDetails;
      }
  }
  </code></pre>
  
  <p>I've been searching a lot, but I can't figure out why the <code>UserServiceImpl</code> is not called. I debbuged and always <code>ClientServiceImpl</code> is being called to fetch for client AND users. 
  After sending a request to <code>/api/oauth/token</code>, with proper parameters (user and client stored in database), I get:</p>
  
  <pre><code>{"error":"unauthorized","error_description":"Incorrect result size: expected 1, actual 0"}
  </code></pre>
  
  <p>I'm using
  - Spring Framework 4.2.5.RELEASE
  - Spring Mvc 4.2.5.RELEASE, 
  - Spring Security 4.1.0.RC1,
  - Spring Security Oauth2 2.0.9.RELEASE</p>