我可以使用以下配置成功将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);
}
.....
}
这是我试过的
以下代码未能找到ClientDetailsService
不是instanceof
或assignableFrom
JdbcClientDetailsService
或ClientRegistrationService
@Controller 公共类ClientPortalApplication {
private ClientRegistrationService clientService;
@Autowired
public void setClientDetailsService(ClientDetailsService clientDetailsService) {
if (clientDetailsService instanceof JdbcClientDetailsService)) {
clientService = (ClientRegistrationService) clientDetailsService;
}
}
......
}
下面的代码连线无法找到ClientRegistrationService
@Controller
public class ClientPortalApplication {
@Autowired
private ClientRegistrationService clientService;
......
}
答案 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);
}
}
}