title: Spring Security OAuth2 /oauth/token endpoint after Basic auth success then no access token
body: <p>I am adding spring security oauth2 to my spring boot application.  I have followed a few of the examples on the web and in github from the spring team (had to make some mods for my use case), but I am still not able to return an oauth2 access token from the /oauth/token endpoint.  I have been working on this for several days and started off trying to do it in JavaConfig, but then switched to xml config and was able to make some progress.  I should note the only config I am doing in xml is the security config and related security beans, all other config is done via JavaConfig.</p>
  
  <p>I am able to successfully authenticate my user (using username &amp; password) using Basic auth, but when it comes to the next step to generate and return the bearer token, this is not happening.  Note, I am using a database for storing my user credentials and to store my access tokens (once they get created). </p>
  
  <p>I am making a POST request using grant_type=password with a client id and client secret.</p>
  
  <p>I am under the impression spring oauth2 would handle creating the access token for me and returning it, but maybe this isn't correct or my xml config maybe wrong? Many thanks for any assistance!  </p>
  
  <p>Here is my security config xml below:</p>
  
  <pre><code>&lt;http pattern="/oauth/token" 
      create-session="stateless"        
      authentication-manager-ref="authenticationManager"
      xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;intercept-url pattern="/**" method="GET" access="ROLE_DENY" /&gt;
      &lt;intercept-url pattern="/**" method="PUT" access="ROLE_DENY" /&gt;
      &lt;intercept-url pattern="/**" method="DELETE" access="ROLE_DENY" /&gt;
      &lt;intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" /&gt;
      &lt;anonymous enabled="false" /&gt;
      &lt;http-basic entry-point-ref="clientAuthenticationEntryPoint" /&gt;
      &lt;!-- include this only if you need to authenticate clients via request 
          parameters&gt;
      &lt;custom-filter ref="clientCredentialsTokenEndpointFilter"
          after="BASIC_AUTH_FILTER" /--&gt;
      &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt;
  &lt;/http&gt;
  
  &lt;http pattern="/**" create-session="never" 
      entry-point-ref="oauthAuthenticationEntryPoint" 
      xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;!-- authentication-manager-ref="authenticationManager"
      access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"--&gt;
      &lt;anonymous enabled="false" /&gt;
      &lt;intercept-url pattern="/**" 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;bean id="oauthAuthenticationEntryPoint"
      class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;!-- property name="realmName" value="f2rRealm" /--&gt;
  &lt;/bean&gt;
  
  &lt;bean id="clientAuthenticationEntryPoint"
      class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="f2r/client" /&gt;
      &lt;property name="typeName" value="Basic" /&gt;
  &lt;/bean&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;bean id="oauthAccessDeniedHandler"
      class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /&gt;
  
  &lt;authentication-manager alias="authenticationManager"
      xmlns="http://www.springframework.org/schema/security" &gt;
      &lt;!-- authentication-provider user-service-ref="clientDetailsUserService"  /--&gt;
      &lt;authentication-provider ref="authenticationProvider" /&gt;
  &lt;/authentication-manager&gt;
  
  &lt;bean id="clientDetails" class="com.f2r.security.oauth2.F2RJdbcClientDetailsService" &gt;
      &lt;constructor-arg ref="dataSource" /&gt;
      &lt;property name="clientId" value="f2r" /&gt;
      &lt;property name="clientSecret" value="f2rsecret" /&gt;
      &lt;property name="passwordEncoder" ref="passwordEncoder" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- bean id="clientDetailsService"
      class="com.f2r.security.oauth2.F2RJdbcClientDetailsService"&gt;
      &lt;constructor-arg ref="dataSource" /&gt;
      &lt;property name="clientDetails" ref="clientDetails" /&gt;
      &lt;property name="passwordEncoder" ref="passwordEncoder" /&gt;
  &lt;/bean--&gt;
  
  &lt;bean id="clientDetailsUserService"
      class="com.f2r.security.oauth2.F2RClientDetailsUserDetailsService"&gt;
      &lt;constructor-arg ref="clientDetails" /&gt;
      &lt;property name="passwordEncoder" ref="passwordEncoder" /&gt;
  &lt;/bean&gt;    
  
  
  &lt;authentication-manager id="userAuthenticationManager"
      xmlns="http://www.springframework.org/schema/security"&gt;
      &lt;!-- authentication-provider ref="customUserAuthenticationProvider" /--&gt;
      &lt;authentication-provider ref="authenticationProvider" /&gt;
  &lt;/authentication-manager&gt;
  
  &lt;!-- bean id="customUserAuthenticationProvider" class="com.f2r.security.F2RAuthenticationProvider"&gt;
      &lt;property name="userDetailsService" ref="userDetailsService" /&gt;
  &lt;/bean--&gt;
  
  &lt;bean id="authenticationProvider" class="com.f2r.security.F2RAuthenticationProvider"&gt;
      &lt;!-- property name="userDetailsService" ref="userDetailsService" /--&gt;
      &lt;property name="userDetailsService" ref="clientDetailsUserService" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="userDetailsService" class="com.f2r.security.F2RUserDetailsService" /&gt;
  
  &lt;bean id="userApprovalHandler" 
      class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler"&gt;
      &lt;property name="tokenStore" ref="tokenStore" /&gt;
      &lt;property name="clientDetailsService" ref="clientDetails" /&gt; 
      &lt;property name="requestFactory" ref="requestFactory" /&gt;  
  &lt;/bean&gt;
  
  &lt;bean id="requestFactory"
      class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory"&gt;
      &lt;constructor-arg ref="clientDetails" /&gt;
  &lt;/bean&gt;
  
  &lt;oauth:authorization-server
      client-details-service-ref="clientDetails" 
      token-services-ref="tokenServices" 
      token-endpoint-url="/oauth/token" 
      authorization-endpoint-url="/oauth/authorize" &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:password /&gt;
  &lt;/oauth:authorization-server&gt;
  
  &lt;oauth:resource-server id="resourceServerFilter"
      token-services-ref="tokenServices" /&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="clientDetailsService" ref="clientDetails" /&gt;
  &lt;/bean&gt;     
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;mvc:default-servlet-handler /&gt;
  
  &lt;sec:global-method-security
      pre-post-annotations="enabled" proxy-target-class="true"&gt;
      &lt;!--you could also wire in the expression handler up at the layer of the 
          http filters. See https://jira.springsource.org/browse/SEC-1452 --&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;
  </code></pre>
  
  <p></p>