prptn
prptn

Reputation: 299

How to configure Spring Security OAuth 2.0 client store to database

I found a tutorial about Spring REST Service OAuth on https://github.com/royclarkson/spring-rest-service-oauth

But I wonder how to configure client stored to database, so I can manage easily. In the tutorial client configuration store inMemory at class OAuth2ServerConfiguration.java

    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        // @formatter:off
        clients.inMemory().withClient("clientapp")
                .authorizedGrantTypes("password", "refresh_token")
                .authorities("USER").scopes("read", "write")
                .resourceIds(RESOURCE_ID).secret("123456");
        // @formatter:on
    }

Upvotes: 8

Views: 19304

Answers (2)

prptn
prptn

Reputation: 299

@OhadR thank you for your answer, really appreciete it!

I acctually found the answer through this thread: error in Spring AuthorizationServerConfigurerAdapter when assigning Jdbc datastore to ClientDetailsService

To do this I only need two step:

  1. create table that represent clientdetails
   CREATE TABLE oauth_client_details (
      client_id VARCHAR(256) PRIMARY KEY,
      resource_ids VARCHAR(256),
      client_secret VARCHAR(256),
      scope VARCHAR(256),
      authorized_grant_types VARCHAR(256),
      web_server_redirect_uri VARCHAR(256),
      authorities VARCHAR(256),
      access_token_validity INTEGER,
      refresh_token_validity INTEGER,
      additional_information VARCHAR(4096),
      autoapprove VARCHAR(256)
    );
  1. defined JDBC configuration
DataSource dataSource = DataSourceBuilder.create()
    .driverClassName("com.mysql.jdbc.Driver")
    .url("jdbc:mysql://localhost:3306/gsrestdb").username("***").password("***").build();

    clients.jdbc(dataSource);

Upvotes: 9

OhadR
OhadR

Reputation: 8859

I believe that this is the answer that you are looking for:

https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/client/JdbcClientDetailsService.java

This is Spring-oAuth Impl class for JDBC.

HTH

Upvotes: 1

Related Questions