Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
like image 499
prptn Avatar asked Apr 15 '15 10:04

prptn


People also ask

How does OAuth2 2.0 work in spring boot?

In Spring boot, we have one mechanism which helps us to do Authorization; this is called as oauth2. 0; by the use of this, we can easily authorize the interaction between two services. The main purpose of oauth2 is to authorize two services on behalf of the user who has access to the resource.

Does Spring Security using OAuth2?

Spring Security provides comprehensive OAuth 2 support.

How does OAuth2 work in REST API spring boot?

It works by delegating user authentication to the service that hosts the user account and authorizing third-party applications to access the user account. Oauth2 provides authorization flows for web and desktop applications, and mobile devices.


1 Answers

@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);
like image 121
prptn Avatar answered Oct 30 '22 02:10

prptn