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 {
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                    .authorizedGrantTypes("password", "refresh_token")
                    .scopes("read", "write")


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.





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:

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


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



All Articles