简体   繁体   English

用jdbc数据源连接ClientRegistrationService

[英]Wiring ClientRegistrationService with jdbc datasource

I could successfully set the jdbc datasource to Spring OAuth2 using the following configuration. 我可以使用以下配置将jdbc数据源成功设置为Spring OAuth2。 However I am struggling to wire ClientRegistrationService while it was easy to wire ClientDetailsService . 但是,尽管要连接ClientDetailsService很容易,但我仍在努力连接ClientRegistrationService

@Configuration
@EnableAuthorizationServer
protected static class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private DataSource dataSource;

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

Here is what I tried 这是我尝试过的

  1. Below code fails to find the ClientDetailsService is not instanceof or of assignableFrom JdbcClientDetailsService or ClientRegistrationService 下面的代码无法找到ClientDetailsService不是instanceofassignableFrom JdbcClientDetailsServiceClientRegistrationService

    @Controller public class ClientPortalApplication { @Controller公共类ClientPortalApplication {

     private ClientRegistrationService clientService; @Autowired public void setClientDetailsService(ClientDetailsService clientDetailsService) { if (clientDetailsService instanceof JdbcClientDetailsService)) { clientService = (ClientRegistrationService) clientDetailsService; } } ...... 

    } }

  2. Below code wiring fails on finding a bean of type ClientRegistrationService 下面的代码连接未能找到类型为ClientRegistrationService的bean

:

@Controller
public class ClientPortalApplication {

    @Autowired
    private ClientRegistrationService clientService;

    ......
}

The ClientDetailsService created in yout AuthorizationServerConfigurerAdapter is not a bean therefore can't be injected. AuthorizationServerConfigurerAdapter创建的ClientDetailsService不是bean,因此无法注入。 A solution is to create a bean JdbcClientDetailsService inject it in the AuthorizationServerConfigurerAdapter and you will be able to inject it anywhere else: 一种解决方案是创建一个JdbcClientDetailsService Bean,将其注入AuthorizationServerConfigurerAdapter ,您将能够将其注入其他任何地方:

@Configuration
public class MyConfiguration {
    @Autowired
    private DataSource dataSource;
    @Bean
    public JdbcClientDetailsService jdbcClientDetailsService() {
        return new JdbcClientDetailsService(dataSource);
    }

    @Configuration
    @EnableAuthorizationServer
    protected class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
        @Autowired
        private JdbcClientDetailsService jdbcClientDetailsService;
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.withClientDetails(jdbcClientDetailsService);
        }
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM