'Spring Oauth2 JDBC Client configuration add same client multiple times

I am configuring an OAuth2 authorization server in a Spring project. Here is configuration.

 @Override
public void configure(ClientDetailsServiceConfigurer clients) 
  throws Exception {
    clients.jdbc(dataSource)
           .withClient("user")
           .secret("secret")
           .scopes("read", "write")
           .autoApprove(true)
           .authorizedGrantTypes(
            "password","authorization_code", "refresh_token")
        }

The problem is that each time I restart application, it tries to add those clients in database, which I don't want. I am getting the unique constraint violation exception. How can I configure it to only add the clients only if they not already exists?

Thanks.



Solution 1:[1]

please find my answer it will works without any errors : https://stackoverflow.com/a/57460575/9963016

find the below code

@Override public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {

    JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);

    if(!jdbcClientDetailsService.listClientDetails().isEmpty() ) {          
    jdbcClientDetailsService.removeClientDetails(CLIEN_ID);     
    }

    if(jdbcClientDetailsService.listClientDetails().isEmpty() ) {
        configurer.jdbc(dataSource).withClient(CLIEN_ID).secret(encoder.encode(CLIENT_SECRET))
        .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT)
        .scopes(SCOPE_READ, SCOPE_WRITE, TRUST).accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS)
        .refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS).and().build();                
    }       
    configurer.jdbc(dataSource).build().loadClientByClientId(CLIEN_ID); 
}

Solution 2:[2]

I solved this problem by first explicitly removing the client details and then re-adding them like so:

// Create client details
BaseClientDetails clientDetails = new BaseClientDetails();
clientDetails.setClientId("user");
clientDetails.setClientSecret("secret");
clientDetails.setScope(Arrays.asList("read", "write"));
clientDetails.setAuthorizedGrantTypes(Arrays.asList("password","authorization_code", "refresh_token"));

// Remove and re-add details
JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
try {
    jdbcClientDetailsService.removeClientDetails(clientDetails.getClientId());
} catch (NoSuchClientException ignored) {
}
jdbcClientDetailsService.addClientDetails(clientDetails);

// Configure clients
clients.withClientDetails(jdbcClientDetailsService);

You can use this code to fully replace the body of the method included in the original question.

Solution 3:[3]

please find my answer it will works without any errors :

find the below code

@Override
public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {

    JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);

    if(!jdbcClientDetailsService.listClientDetails().isEmpty() ) {          
    jdbcClientDetailsService.removeClientDetails(CLIEN_ID);     
    }

    if(jdbcClientDetailsService.listClientDetails().isEmpty() ) {
        configurer.jdbc(dataSource).withClient(CLIEN_ID).secret(encoder.encode(CLIENT_SECRET))
        .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT)
        .scopes(SCOPE_READ, SCOPE_WRITE, TRUST).accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS)
        .refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS).and().build();                
    }       
    configurer.jdbc(dataSource).build().loadClientByClientId(CLIEN_ID); 
}

Solution 4:[4]

Reason: When the application comes up, it inserts what's there in your withClient() into the oauth_client_details table so please remove that and restart your application and it should work.

As you have already ran your application, it inserted the row in the oauth_client_details table.

Solution: Just have this line your method. It will find the row in that table.

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception 
{
    clients.jdbc(dataSource);
}

Suppose for the first time you start your application with above line then I guess you can directly add the entry into that table or it can be added by the application as well.

I manually added it for testing purposes. It worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 forkdbloke
Solution 4 Laurel