title: Custom UserDetailsService in spring-security
body: <p>I cant proceed further into my project because of an strange error. I am trying to implement spring-security and spring data JPA for my application, which exposes only REST api.
  Now, when I am trying to implement my custom UserDetailsService class, as mentioned below, the parameter username in method loadUserByUsername is not the username I am passing as a POST request parameter, rather its the client_id parameter.
  Can someone please suggest as what am I doing wrong?</p>
  
  <pre class="lang-java prettyprint-override"><code>public class CustomUserDetailsService implements UserDetailsService {
  
      private  ClientDetailsService clientDetailsService;
      private String emptyPassword = "";
  
      public CustomUserDetailsService() {
  
      }
  
      public CustomUserDetailsService(ClientDetailsService clientDetailsService) {
          this.clientDetailsService = clientDetailsService;
      }
  
      private List&lt;GrantedAuthority&gt; getAuthorities(String role) {
          List&lt;GrantedAuthority&gt; authList = new ArrayList&lt;GrantedAuthority&gt;();
          authList.add(new SimpleGrantedAuthority("ROLE_USER"));
  
          // you can also add different roles here
          // for example, the user is also an admin of the site, then you can add
          // ROLE_ADMIN
          // so that he can view pages that are ROLE_ADMIN specific
          if (role != null &amp;&amp; role.trim().length() &gt; 0) {
              if (role.equals("admin")) {
                  authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
              }
          }
  
          return authList;
      }
  
      @Override
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException {
          ClientDetails clientDetails = clientDetailsService
                  .loadClientByClientId(username);
          String clientSecret = clientDetails.getClientSecret();
          if (clientSecret == null || clientSecret.trim().length() == 0) {
              clientSecret = emptyPassword;
          }
          return new User(username, clientSecret, clientDetails.getAuthorities());
      }
  }
  </code></pre>
  
  <p>My spring-security configuration is as following:</p>
  
  <pre class="lang-xml prettyprint-override"><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: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.1.xsd
          http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd "&gt;
  
  
      &lt;http pattern="/oauth/token" create-session="stateless"
          authentication-manager-ref="authenticationManager"
          xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" /&gt;
          &lt;anonymous enabled="false" /&gt;
          &lt;http-basic entry-point-ref="clientAuthenticationEntryPoint" /&gt;
          &lt;custom-filter ref="clientCredentialsTokenEndpointFilter"
              before="BASIC_AUTH_FILTER" /&gt;
          &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt;
      &lt;/http&gt;
  
      &lt;http pattern="/api/**" create-session="never"
          entry-point-ref="oauthAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;anonymous enabled="false" /&gt;
          &lt;intercept-url pattern="/api/**" access="IS_AUTHENTICATED_FULLY" /&gt;
          &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /&gt;
          &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt;
      &lt;/http&gt;
  
      &lt;http pattern="/logout" create-session="never"
          entry-point-ref="oauthAuthenticationEntryPoint" xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;anonymous enabled="false" /&gt;
          &lt;intercept-url pattern="/logout" method="GET" /&gt;
          &lt;sec:logout invalidate-session="true" logout-url="/logout"
              success-handler-ref="logoutSuccessHandler" /&gt;
          &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /&gt;
          &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt;
      &lt;/http&gt;
  
      &lt;bean id="logoutSuccessHandler" class="com.monsor.feasthub.security.LogoutImpl"&gt;
          &lt;property name="tokenstore" ref="tokenStore" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="oauthAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint" /&gt;
  
      &lt;bean id="applicationContextProvder" class="com.monsor.feasthub.util.ApplicationContextProvider" /&gt;
  
      &lt;bean id="clientAuthenticationEntryPoint"
          class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
          &lt;property name="realmName" value="springsec/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;bean id="clientCredentialsTokenEndpointFilter"
          class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"&gt;
          &lt;property name="authenticationManager" ref="authenticationManager" /&gt;
      &lt;/bean&gt;
  
      &lt;authentication-manager alias="authenticationManager"
          xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;authentication-provider user-service-ref="clientDetailsUserService" /&gt;
      &lt;/authentication-manager&gt;
  
  
      &lt;bean id="clientDetailsUserService" class="com.monsor.feasthub.security.CustomUserDetailsService"&gt;
          &lt;constructor-arg ref="clientDetails" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="clientDetails" class="com.monsor.feasthub.security.ClientDetailsServiceImpl" /&gt;
  
      &lt;oauth:authorization-server
          client-details-service-ref="clientDetails" token-services-ref="tokenServices"&gt;
          &lt;oauth:authorization-code /&gt;
          &lt;oauth:implicit /&gt;
          &lt;oauth:refresh-token /&gt;
          &lt;oauth:client-credentials /&gt;
          &lt;oauth:password authentication-manager-ref="userAuthenticationManager" /&gt;
      &lt;/oauth:authorization-server&gt;
  
      &lt;authentication-manager id="userAuthenticationManager"
          xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;authentication-provider ref="customUserAuthenticationProvider" /&gt;
  
      &lt;/authentication-manager&gt;
  
      &lt;bean id="customUserAuthenticationProvider"
          class="com.monsor.feasthub.security.CustomUserAuthenticationProvider" /&gt;
  
  
      &lt;oauth:resource-server id="resourceServerFilter"
          resource-id="springsec" token-services-ref="tokenServices" /&gt;
  
      &lt;bean id="tokenStore"
          class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" /&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="300000" /&gt;
          &lt;property name="clientDetailsService" ref="clientDetails" /&gt;
      &lt;/bean&gt;
  
  
      &lt;mvc:annotation-driven /&gt;   &lt;!-- Declares explicit support for annotation-driven MVC controllers @RequestMapping, 
          @Controller --&gt;
  
      &lt;mvc:default-servlet-handler /&gt;
  
      &lt;bean id="MyResource" class="com.monsor.feasthub.resources.MyResource" /&gt;
  
  &lt;/beans&gt;
  </code></pre>