web-dev-qa-db-ja.com

SpringOAuth2認証サーバーに複数のクライアントを追加する

Spring OAuth認証サーバーがあり、複数のclient(id)のサポートを追加したい。クライアントを次のように構成しました。

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); 

最初のクライアントでアクセストークンを取得できますが、2番目のクライアントでアクセストークンを取得しようとすると、このエラーが発生します。

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

複数のクライアントを追加することは可能ですか?その方法は?それでは、データベースからクライアントを読み取る方法は?

17
dplesa

複数のinMemoryビルダーを使用せず、代わりに1つのwithClient内に複数の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");
}
20
Ali Dehghani

inMemorybuilder 構成ありの場合(独自の構成を定義する必要があります):

 @Override
    public void configure ( ClientDetailsServiceConfigurer clients ) throws Exception {
        // @formatter:off
        InMemoryClientDetailsServiceBuilder inMemoryBuilder = clients.inMemory ();
        for (String clientKey: authServerProperties.getClient ().keySet ()) {
            OAuthClientProperties client = authServerProperties.getClient ().get ( clientKey );
            inMemoryBuilder
                .withClient ( client.getClientId () )
                .secret ( client.getClientSecret () )
                .scopes ( client.getScopes () == null ? new String[] {"openid"} : client.getScopes () )
                .authorizedGrantTypes ( client.getAuthorizedGrandTypes () == null ? "client_credentials" : client.getAuthorizedGrandTypes () );
        }

        // @formatter:on
    }

2つの追加クラス:

@ConfigurationProperties ( prefix = "my-authorization-server" )
public class AuthServerProperties 

    private final Map<String, OAuthClientProperties> client = new HashMap<> ();

    ...

    public Map<String, OAuthClientProperties> getClient () {
        return client;
    }

    ...

}


public class OAuthClientProperties {

    private String clientId;

    private String clientSecret;

    private String[] scopes;

    private String authorizedGrandTypes;

    public String getClientId () {
        return clientId;
    }

    public void setClientId ( String clientId ) {
        this.clientId = clientId;
    }

    public String getClientSecret () {
        return clientSecret;
    }

    public void setClientSecret ( String clientSecret ) {
        this.clientSecret = clientSecret;
    }

    public String[] getScopes () {
        return scopes;
    }

    public void setScopes ( String[]  scopes ) {
        this.scopes = scopes;
    }

    public String getAuthorizedGrandTypes () {
        return authorizedGrandTypes;
    }

    public void setAuthorizedGrandTypes ( String authorizedGrandTypes ) {
        this.authorizedGrandTypes = authorizedGrandTypes;
    }

}

そして最後に、プロパティには次のようなものがあります。

my-authorization-server.client.foo.client-id=foo-client
my-authorization-server.client.foo.client-secret=foo-client-supersecret
my-authorization-server.client.foo.scopes=read

my-authorization-server.client.bar.client-id=bar-client
my-authorization-server.client.bar.client-secret=bar-client-verysupersecret
my-authorization-server.client.bar.scopes=read,write
1
user2310395