8

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
    }
OhadR
  • 8,276
  • 3
  • 47
  • 53
prptn
  • 299
  • 1
  • 6
  • 18

2 Answers2

9

@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);
Community
  • 1
  • 1
prptn
  • 299
  • 1
  • 6
  • 18
  • 1
    I think you need to create all tables listed here https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql , except the ClientDetails table – soulmachine Nov 16 '15 at 10:52
  • @soulmachine : yes, you are right I need to create table schema, thanks for the link tough. – prptn Nov 17 '15 at 02:33
  • 2
    @prptn Where does the `DataSourceBuilder.create()` code go? In which class and method? And how do you tell OAuth to use the database? – Don Rhummy Jul 23 '17 at 22:01
  • @prptn you only need additional tables if you wish to store tokens in database else you don't need to create other tables. – Ravik Oct 23 '18 at 09:20
1

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

OhadR
  • 8,276
  • 3
  • 47
  • 53
  • 10
    I'm not sure that linking to the source code is the most helpful way to answer a question on how to implement something. – SunshinyDoyle Jan 13 '16 at 19:52