Spring Oauth2 JDBC Client configuration add same client multiple times

给你一囗甜甜゛ 提交于 2019-12-08 04:02:10

问题


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.


回答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); 
}



回答2:


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

As you have already ran your application, it inserted row in 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 entry into that table or it can be added by application as well.

I manually added for testing purpose.

Hope this helps to you. It worked for me.




回答3:


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); 
}



回答4:


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.



来源:https://stackoverflow.com/questions/40154466/spring-oauth2-jdbc-client-configuration-add-same-client-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!