Adding more then one client to the Spring OAuth2 Auth Server

dplesa picture dplesa · Mar 1, 2016 · Viewed 7.5k times · Source

I have Spring OAuth Authorization server and I want to add support for more then one client(id). I configured clients like this:

clients
            .inMemory().withClient(client).secret(clientSecret)
            .resourceIds(resourceId)
            .authorizedGrantTypes("client_credentials", "password", "refresh_token", "implicit", "authorization_code")
            .authorities("ROLE_USER")
            .scopes("read", "write")
            .autoApprove(true)
            .and()
            .inMemory().withClient("acme").secret("acmesecret")
            .resourceIds(resourceId)
            .authorizedGrantTypes("client_credentials", "password", "refresh_token", "implicit", "authorization_code")
            .authorities("ROLE_USER_ACME")
            .scopes("read", "write")
            .autoApprove(true); 

I can get access token with first client, but i get this error when trying to get access token with second client:

{
  "timestamp": 1456822249638,
  "status": 401,
  "error": "Unauthorized",
  "message": "Bad credentials",
  "path": "/oauth/token"
}

Is it possible to add more then one client and how to do it? Allso, how to read clients from a database?

Answer

Ali Dehghani picture Ali Dehghani · Mar 1, 2016

Do not use multiple inMemory builders, instead concatenate multiple withClients inside one inMemory:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
                .withClient("first")
                .secret("secret")
                .scopes("read")
                .authorizedGrantTypes("password")
            .and()
                .withClient("sec")
                .secret("secret")
                .scopes("read")
                .authorizedGrantTypes("password");
}