Spring Oauth2 JDBC Client配置多次添加相同的客户端 [英] Spring Oauth2 JDBC Client configuration add same client multiple times

查看:107
本文介绍了Spring Oauth2 JDBC Client配置多次添加相同的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Spring 项目中配置 OAuth2 授权服务器.这里是配置.

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?

谢谢.

推荐答案

请找到我的答案它无需任何错误:

please find my answer it will works without any errors :

找到下面的代码

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

这篇关于Spring Oauth2 JDBC Client配置多次添加相同的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆