Migrate spring oauth db from inmemory to jdbc

I am trying to add oauth2 support to my java rest service,

I managed to get it to work in memory:

protected static class MyAuthorizationServerConfigurerAdapter extends
            AuthorizationServerConfigurerAdapter {
...
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
            .inMemory()
                .withClient("myapp")
                    .authorizedGrantTypes("password", "refresh_token")
                    .authorities("USER")
                    .scopes("read", "write")
                    .resourceIds(RESOURCE_ID)
                    .secret("mysecret");
        }
...
    }

      

problem, every time the server is restarted, the db is lost. so I would like to connect it and an existing standalone SQL db, any idea or tutorial how can this be done?

I found there is a jdbc option, but I can't find a way to get it to work.

clients.jdbc(dataSource)

      

thank

+3


source to share


1 answer


Are you sure you want to put the client configuration in the database? This will allow you to dynamically set up new clients.

I think you want your tokens to be stored in the database, so if you restart the server, the client won't lose the session.

This can be done with the following code:

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception }
    endpoints.tokenStore(new JdbcTokenStore(dataSource)).userApprovalHandler(userApprovalHandler)
                    .authenticationManager(authenticationManager);
}

      



Here is the code to create the database tables:

CREATE TABLE `oauth_access_token` (
  `token_id` varchar(255) DEFAULT NULL,
  `token` blob,
  `authentication_id` varchar(255) DEFAULT NULL,
  `user_name` varchar(255) DEFAULT NULL,
  `client_id` varchar(255) DEFAULT NULL,
  `authentication` blob,
  `refresh_token` varchar(255) DEFAULT NULL
);

CREATE TABLE `oauth_refresh_token` (
  `token_id` varchar(256) DEFAULT NULL,
  `token` blob,
  `authentication` blob
);

      

Source: https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql

+3


source







All Articles