Getting invalid scope when requesting for oauth2 access token
<p>I have an oauth service in my server which performs the authentication operation and issues access token when request comes for valid users.Then when i tried to request to this service using a command line curl request i am getting below error.</p>

<pre><code>"detailMessage":"Invalid scope: read,write,trust","cause":{"additionalInformation":{"scope":"read trust write"}
</code></pre>

<p>Below is my request which results the error.</p>

<pre><code>curl -X POST http://localhost:8080/MyProjectOauth/oauth/token -H “Accept: application/json” -d "grant_type=password&amp;client_id=client1&amp;client_secret=client1&amp;username=user1&amp;password=user1&amp;scope=read,write,trust"
</code></pre>

<p>If i tried this request without the scope then getting below error.</p>

<pre><code>"detailMessage":"Bad credentials","cause":{"detailMessage":"Bad credentials"
</code></pre>

<p>Below is my spring security configuration file.</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: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-4.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="clientAuthenticationManager"
            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;!-- 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;!-- The OAuth2 protected resources are separated out into their own block so we can deal with authorization and error handling
         separately. This isn't mandatory, but it makes it easier to control the behaviour. --&gt;
      &lt;http pattern="/protected/**" 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="/protected/**" access="ROLE_USER"/&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="test"/&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="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.access.vote.AuthenticatedVoter"/&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="passwordEncoder"
            class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"&gt;
          &lt;constructor-arg name="strength" value="11"/&gt;
      &lt;/bean&gt;

      &lt;authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"&gt;
          &lt;authentication-provider user-service-ref="userService"&gt;
              &lt;password-encoder ref="passwordEncoder"/&gt;
          &lt;/authentication-provider&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;!-- Used for the persistenceof tokens (currently an in memory implementation) --&gt;
      &lt;bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore"&gt;
  &lt;!--         &lt;constructor-arg ref="dataSource"/&gt;    --&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="3600" /&gt;
      &lt;property name="refreshTokenValiditySeconds" value="5270400"&gt;&lt;/property&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;!-- authorization-server aka AuthorizationServerTokenServices is an interface that defines everything necessary for token management --&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.example.myproject.ser.ClientService"&gt;
      &lt;/bean&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;

  &lt;/beans&gt;
</code></pre>

<p>ClientService.java :</p>

<pre><code>      import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.oauth2.provider.ClientDetails;
  import org.springframework.security.oauth2.provider.ClientDetailsService;
  import org.springframework.security.oauth2.provider.ClientRegistrationException;
  import org.springframework.security.oauth2.provider.client.BaseClientDetails;
  import org.springframework.stereotype.Component;

  import com.example.myproject.rep.OauthRepository;

  @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>OauthRepository.java :</p>

<pre><code>      @Repository
  @Transactional
  public class OauthRepository {

    @Autowired
    private SessionFactory sessionFactory;

    private org.hibernate.Session getCurrentSession(){
      return sessionFactory.getCurrentSession();
    }



      public BaseClientDetails getByClientId(String clientId) {
        Query query=getCurrentSession().createQuery("FROM OauthClientDetails WHERE clientId=:clientId");
        query.setParameter("clientId", clientId);
        List&lt;OauthClientDetails&gt; getClient=query.list();

        OauthClientDetails oauthClient=getClient.get(0);
        BaseClientDetails details = new BaseClientDetails(oauthClient.getClientId(),oauthClient.getResourceIds(),oauthClient.getScope(),oauthClient.getAuthorizedGrantTypes(),oauthClient.getAuthorities());
        details.setClientSecret(oauthClient.getClientSecret());

          return details;



      }
       } 
</code></pre>

<p>Below is my database client table data.</p>

<pre><code>         CREATE TABLE oauth_client_details (
      client_id varchar(50) NOT NULL,
      resource_ids varchar(256) DEFAULT NULL,
      client_secret varchar(256) DEFAULT NULL,
      scope varchar(256) DEFAULT NULL,
      authorized_grant_types varchar(256) DEFAULT NULL,
      web_server_redirect_uri varchar(256) DEFAULT NULL,
      authorities varchar(256) DEFAULT NULL,
      access_token_validity int(11) DEFAULT NULL,
      refresh_token_validity int(11) DEFAULT NULL,
      additional_information varchar(4096) DEFAULT NULL,
      autoapprove varchar(4096) DEFAULT NULL,
      PRIMARY KEY (client_id)
    );


INSERT INTO oauth_client_details(client_id, resource_ids, client_secret, scope, authorized_grant_types, authorities, access_token_validity, refresh_token_validity)
VALUES ('client1', 'rest_api', 'client1', 'read,write,trust', 'password,authorization_code,refresh_token,implicit', 'ROLE_ANDROID', '5', '1000');
</code></pre>

<p>Please help me to solve the problem</p>