Spring Oauth2 JDBC Client configuration add same client multiple times
Asked Answered
G

4

1

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.

Gage answered 20/10, 2016 at 12:28 Comment(0)
C
1

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.

Chromosome answered 16/11, 2018 at 22:12 Comment(0)
C
1

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); 
}
Coastland answered 12/8, 2019 at 10:49 Comment(2)
Please edit the markdown of your code put your answer always in context instead of just pasting code. See here for more details.Spacecraft
@Spacecraft ok sir thanks for the information. I am new to StackOverflow.I will Improve myself.Coastland
C
1

please find my answer it will works without any errors : https://mcmap.net/q/656507/-how-to-add-a-client-using-jdbc-for-clientdetailsserviceconfigurer-in-spring

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); 
}
Coastland answered 12/8, 2019 at 11:48 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Blimey
I
0

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.

Isoagglutinin answered 8/1, 2017 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.