title: No Such Client Exception Spring Oauth2
body: <p>I am trying to implement Spring Security OAuth2 using Java config. </p>
  
  <p>My usecase requires the use of password grant_type. </p>
  
  <p>I have configured this so far without the need for a web.xml and would prefer to keep it that way</p>
  
  <p>Versions I am using:</p>
  
  <ul>
  <li>Spring Framework: 4.1.6 </li>
  <li>Spring Security: 4.0.1 </li>
  <li>Spring Security OAuth:2.0.7</li>
  </ul>
  
  <p>To make explaining this easier I have enabled GET on the token endpoint   </p>
  
  <pre><code>    @Override
      public void configure
          (AuthorizationServerEndpointsConfigurer endpoints) throws Exception
      {
              endpoints
                  .tokenStore(tokenStore)
                  .authenticationManager(authenticationManager)
                  .allowedTokenEndpointRequestMethods(HttpMethod.GET); //&lt;-- Enable GET
      }
  </code></pre>
  
  <p>The request that I am making is as follows:</p>
  
  <pre><code>http://localhost:8080/project/oauth/token?
      client_id=testClient&amp;
      grant_type=password&amp;
      username=user&amp;
      password=password
  </code></pre>
  
  <p>The header includes an Authorization header that contains the encoded version of: </p>
  
  <pre><code>Username: user
  Password: password
  </code></pre>
  
  <p>The exception I get is:</p>
  
  <pre><code>HTTP Status 500 - Request processing failed; nested exception is
     org.springframework.security.oauth2.provider.NoSuchClientException:
     No client with requested id: user
  </code></pre>
  
  <p>From the exception description it appears that OAuth is looking in the ClientDetailsService for the client: <em>user</em>. However <em>user</em> is a user credential. I am obviously missunderstanding something about the configuration. </p>
  
  <p>My configuration is as follows;</p>
  
  <p><strong>ServletInitializer.java</strong></p>
  
  <pre><code>public class ServletInitializer extends AbstractDispatcherServletInitializer {
  
      @Override
      protected WebApplicationContext createServletApplicationContext() {
          AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
          context.scan(ClassUtils.getPackageName(getClass()));
          return context;
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[]{"/"};
      }
  
      @Override
      protected WebApplicationContext createRootApplicationContext() {
          return null;
      }
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException{
          super.onStartup(servletContext);
          DelegatingFilterProxy filter = new DelegatingFilterProxy("springSecurityFilterChain");
      filter.setContextAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
          servletContext.addFilter("springSecurityFilterChain", filter).addMappingForUrlPatterns(null, false, "/*");
      }
  }
  </code></pre>
  
  <p><strong>WebMvcConfig.java</strong></p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class WebMvcConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
              configurer.enable();
      }
  }
  </code></pre>
  
  <p><strong>SecurityConfiguration.java</strong></p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception{
          auth.
              inMemoryAuthentication()
              .withUser("user")
              .password("password")
              .roles("USER");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception{
          http
              .authorizeRequests()
              .antMatchers("/Services/*")
              .authenticated()
          .and()
              .httpBasic();
      }
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
  }
  </code></pre>
  
  <p><strong>OAuth2ServerConfig.java</strong></p>
  
  <pre><code>@Configuration
  public class OAuth2ServerConfig {
  
      @Configuration
      @EnableAuthorizationServer
      protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter{
  
          @Autowired 
          private TokenStore tokenStore;
  
          @Autowired
          @Qualifier("authenticationManagerBean")
          private AuthenticationManager authenticationManager;
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
  
              clients
                  .inMemory()
                      .withClient("testClient")
                      .secret("secret")
                      .scopes("read", "write")
                      .authorities("ROLE_CLIENT")
                      .authorizedGrantTypes("password", "refresh_token")
                      .accessTokenValiditySeconds(60)
                      .refreshTokenValiditySeconds(3600);
          }
  
          @Bean
          public TokenStore tokenStore() {
              return new InMemoryTokenStore();
          }
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception{
              endpoints
                  .tokenStore(tokenStore)
                  .authenticationManager(authenticationManager)
                  .allowedTokenEndpointRequestMethods(HttpMethod.GET);
          }
  
          @Override 
          public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  
          }
      }
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  
          @Override
          public void configure(ResourceServerSecurityConfigurer resources){
              resources.resourceId("SomeResourseId").stateless(false);
          }
  
          @Override
          public void configure(HttpSecurity http) throws Exception{
  
              http
              .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
              .and()
                  .authorizeRequests()
                      .antMatchers("/secure/**").access("#oauth2.hasScope('read')");
          }
      }
  }
  </code></pre>
  
  <p>Code in gitrepo for ease of access: <a href="https://github.com/dooffas/springOauth2" rel="nofollow">https://github.com/dooffas/springOauth2</a></p>