Oauth2 with Spring throwing bad credentials
<p>I am trying to implement Oauth2 spring security with Spring 4.1 
I am sending password, username , client_id and client_secret but still i get bad credentials.
the request i send is as follows:</p>

<p><a href="http://localhost:8080/asset-manager/oauth/token?grant_type=password&amp;client_id=restapp&amp;client_secret=restapp&amp;username=ankita&amp;password=ankita" rel="nofollow">http://localhost:8080/asset-manager/oauth/token?grant_type=password&amp;client_id=restapp&amp;client_secret=restapp&amp;username=ankita&amp;password=ankita</a></p>

<p>I have the following files</p>

<p>spring-security.xml</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.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.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;!-- This is default url to get a token from OAuth --&gt;
&lt;http pattern="/oauth/token" create-session="stateless"
    authentication-manager-ref="clientAuthenticationManager"
    xmlns="http://www.springframework.org/schema/security"&gt;
    &lt;intercept-url pattern="/oauth/token" access="isFullyAuthenticated()" /&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;csrf disabled="true" /&gt;
&lt;/http&gt;

&lt;!-- This is where we tells spring security what URL should be protected 
    and what roles have access to them --&gt;
&lt;http pattern="/api/**" create-session="never"
    entry-point-ref="oauthAuthenticationEntryPoint"
    access-decision-manager-ref="accessDecisionManager"
    xmlns="http://www.springframework.org/schema/security"&gt;
    &lt;anonymous enabled="false" /&gt;
    &lt;intercept-url pattern="/api/**" access="hasRole('ROLE_USER')" /&gt;
    &lt;custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /&gt;
    &lt;access-denied-handler ref="oauthAccessDeniedHandler" /&gt;
    &lt;csrf disabled="true" /&gt;
&lt;/http&gt;


&lt;bean id="oauthAuthenticationEntryPoint"
    class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"&gt;
    &lt;property name="realmName" value="test" /&gt;
&lt;/bean&gt;

&lt;bean id="requestFactory"
    class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory"&gt;
    &lt;constructor-arg name="clientDetailsService" ref="clientDetails" /&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;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="accessDecisionManager"
    class="org.springframework.security.access.vote.AffirmativeBased"
    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.access.vote.AuthenticatedVoter" /&gt;
            &lt;bean
                class="org.springframework.security.web.access.expression.WebExpressionVoter"&gt;&lt;/bean&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;bean id="authenticationProcessingFilter"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"&gt;
    &lt;property name="authenticationManager" ref="authenticationManager" /&gt;
&lt;/bean&gt;
&lt;!-- This is simple authentication manager, with a hardcoded user/password 
    combination. We can replace this with a user defined service to get few users 
    credentials from DB --&gt;
&lt;authentication-manager alias="authenticationManager"
    xmlns="http://www.springframework.org/schema/security"&gt;
    &lt;authentication-provider&gt;
        &lt;user-service&gt;
            &lt;user name="ankita" password="ankita" authorities="ROLE_USER" /&gt;
        &lt;/user-service&gt;
    &lt;/authentication-provider&gt;
    &lt;authentication-provider user-service-ref="clientDetailsUserService" /&gt;

&lt;/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;!-- This defined token store, we have used inmemory tokenstore for now 
    but this can be changed to a user defined one --&gt;
&lt;bean id="tokenStore"
    class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" /&gt;

&lt;!-- This is where we defined token based configurations, token validity 
    and other things --&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="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;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;oauth:client-details-service id="clientDetails"&gt;
    &lt;!-- client --&gt;
    &lt;oauth:client client-id="restapp"
        authorized-grant-types="authorization_code,client_credentials"
        authorities="ROLE_USER" scope="read,write,trust" secret="secret" /&gt;

    &lt;oauth:client client-id="restapp"
        authorized-grant-types="password,authorization_code,refresh_token,implicit"
        secret="restapp" scope="read,write,trust" authorities="ROLE_USER" /&gt;

&lt;/oauth:client-details-service&gt;
&lt;bean id="httpSessionSecurityContextRepository"
    class='org.springframework.security.web.context.HttpSessionSecurityContextRepository'&gt;
    &lt;property name='allowSessionCreation' value='false' /&gt;
&lt;/bean&gt;
&lt;bean id="securityContextPersistenceFilter"
    class="org.springframework.security.web.context.SecurityContextPersistenceFilter"&gt;
    &lt;constructor-arg ref="httpSessionSecurityContextRepository" /&gt;
&lt;/bean&gt;
&lt;bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy"&gt;
    &lt;constructor-arg&gt;
        &lt;list&gt;
            &lt;sec:filter-chain pattern="/**"
                filters="securityContextPersistenceFilter" /&gt;
        &lt;/list&gt;
    &lt;/constructor-arg&gt;
&lt;/bean&gt;
&lt;sec:global-method-security
    pre-post-annotations="enabled" proxy-target-class="true"
    authentication-manager-ref="authenticationManager"
    secured-annotations="enabled"&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>

<p>And this is my pom.xml</p>

<pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"&gt;



&lt;servlet&gt;
    &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;
    /WEB-INF/mvc-dispatcher-servlet.xml,        
      /WEB-INF/spring-security.xml
      &lt;/param-value&gt;
&lt;/context-param&gt;

&lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
&lt;/listener&gt; 

&lt;!-- Spring Security --&gt;

&lt;filter&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;listener&gt;
    &lt;listener-class&gt;org.springframework.security.web.session.HttpSessionEventPublisher&lt;/listener-class&gt;
&lt;/listener&gt;
</code></pre>

<p></p>

<p>Unfortunately whenever i send a request it throws this:</p>

<pre><code>{
  "error": "invalid_grant",
  "error_description": "Bad credentials"
 }
</code></pre>

<p>Need Help !</p>