使用jdbc数据源连接ClientRegistrationService

时间:2015-02-13 14:30:57

标签: spring oauth spring-security

我可以使用以下配置成功将jdbc数据源设置为Spring OAuth2。但是,当我很容易连接ClientRegistrationService时,我很难接线ClientDetailsService

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

这是我试过的

  1. 以下代码未能找到ClientDetailsService不是instanceofassignableFrom JdbcClientDetailsServiceClientRegistrationService

    @Controller 公共类ClientPortalApplication {

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

    }

  2. 下面的代码连线无法找到ClientRegistrationService

  3. 类型的bean

    @Controller
    public class ClientPortalApplication {
    
        @Autowired
        private ClientRegistrationService clientService;
    
        ......
    }
    

1 个答案:

答案 0 :(得分:1)

ClientDetailsService中创建的AuthorizationServerConfigurerAdapter不是bean,因此无法注入。一个解决方案是创建一个bean JdbcClientDetailsService将它注入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);
        }
    }    
}