title: "could not obtain access token" error in spring security oauth2
body: <p>In my spring project i have an oauth2 client app running on localhost:9999 and an oauth2 authorization server running on localhost:8080.
  In result, after approval page, i see error page that i don't know what is the problem?
  when press F12 i see that set-cookie done! but /oauth/token not called! and /me also not called! and browser not redirect to localhost:9999.</p>
  
  <p>my client app</p>
  
  <pre><code>package sso.client;
  import java.security.Principal;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @EnableAutoConfiguration
  @Configuration
  @EnableOAuth2Sso
  @RestController
  public class App {
  
    @RequestMapping("/")
    public String home(Principal user) {
      return "Hello " + user.getName();
    }
  
    public static void main(String[] args) {
      new SpringApplicationBuilder(App.class)
          .properties("spring.config.name=client").run(args);
    }
  
  }
  </code></pre>
  
  <p>client.yml</p>
  
  <pre><code>server:
    port: 9999
  security:
    oauth2:
      client:
        client-id: acme
        client-secret: acmesecret
        access-token-uri: http://localhost:8080/oauth/token
        user-authorization-uri: http://localhost:8080/oauth/authorize
      resource:
        user-info-uri: http://localhost:8080/me
  </code></pre>
  
  <p>my authorization server</p>
  
  <pre><code>package sso.raymon;
  
  import java.security.Principal;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  @Configuration
  @EnableAutoConfiguration
  @RestController
  @EnableWebSecurity
  public class App extends WebSecurityConfigurerAdapter
  {
       public static void main(String[] args) {
            SpringApplication.run(App.class, args);
       }
  
       @Configuration
       @EnableAuthorizationServer
       protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter{
  
            @Autowired
            private AuthenticationManager authenticationManager;
  
  
            @Override
            public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
                 endpoints.authenticationManager(authenticationManager);
            }
  
            @Override
              public void configure(AuthorizationServerSecurityConfigurer security)
                      throws Exception {
                  // TODO Auto-generated method stub
                  security.allowFormAuthenticationForClients();
              }
  
            @Override
            public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                 // @formatter:off
                 clients.inMemory()
                 .withClient("acme")
                 .authorizedGrantTypes("authorization_code")
                 .authorities("ROLE_CLIENT")
                 .scopes("read", "write")
                 .secret("acmesecret");
                 // @formatter:on
            } 
       }
       @RequestMapping("/me") 
       public String home(Principal user) {
            return user.getName();
       }
  
       @Configuration
       @EnableResourceServer 
       protected static class ResourceServer extends ResourceServerConfigurerAdapter {
  
            @Override 
            public void configure(HttpSecurity http) throws Exception {
                http
                      .antMatcher("/me")
                      .authorizeRequests().anyRequest().authenticated();
            }
  
       }
  }
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>security.user.name=forough
  security.user.password=m123
  </code></pre>
  
  <p>Error in blow URL:</p>
  
  <blockquote>
    <p>localhost:9999/login?code=xZgYwZ&amp;state=27XzVY</p>
  </blockquote>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Whitelable Error Page
    this application has no explicit mapping for /error, so you are seeing this a fallback.
    There was an unexpected erroe (type=Unauthorized, atatus=401).
    Authentication Failed: could not obtain access token</p>
  </blockquote>