我试图将数据库添加到Spring OAuth Server示例代码。 以下是dataSource配置。Spring OAuth服务器JBDCTokenStore错误
@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.mariadb.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mariadb://localhost:3306/OAuth");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("password");
return driverManagerDataSource;
}
而且EndpointConfig
@Autowired
private DataSource dataSource;
@Bean
public JdbcClientDetailsService clientDetailsService() {
return new JdbcClientDetailsService(dataSource);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService());
}
@Bean
public TokenStore tokenStore() {
//return new InMemoryTokenStore();
return new JdbcTokenStore(dataSource);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore);
endpoints.userApprovalHandler(userApprovalHandler);
endpoints.authenticationManager(authenticationManager);
}
但我得到JbdctokenStore,JdbcClientDetailsService做工精细错误。
错误消息:java.lang.IllegalArgumentException异常:
所致的DataSource需要
所致:org.springframework.beans.factory.BeanCreationException: 错误与名称创建豆 'tokenStore'在类路径中定义 资源 [org/springframework/security/oauth/examples/sparklr/config/OAuth2ServerConfig $ AuthorizationServerConfiguration.class]: bean的实例化失败;嵌套的例外是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法[公共 org.springframework.security.oauth2.provider.token.TokenStore org.springframework.security.oauth.examples.sparklr.config.OAuth2ServerConfig $ AuthorizationServerConfiguration.tokenStore()] 引发异常;嵌套的例外是 java.lang.IllegalArgumentException异常:数据源需要
我无法弄清楚发生什么。 数据库和表是所有罚款。(我用的是SQL文件样本给了。)
@Bean
public TokenStore tokenStore() {
//return new InMemoryTokenStore();
return new JdbcTokenStore(dataSource());
}
@Bean
public DriverManagerDataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.mariadb.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mariadb://localhost:3306/OAuth");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("a89019");
return driverManagerDataSource;
}
我刚下它和改变数据源添加新豆到数据源(), 和它的工作。 我无法得到它,为什么JdbcClientDetailsService与orignal一起工作。 但JdbcTokenStore需要一个新的。 如何更改这部分代码,代码由于代码重复而显得杂乱无章。
您是否在'JdbcTokenStore'类上有'@ Configuration'注释? – harshavmb
不,但我通过在其下添加重复代码来解决错误。我不知道它为什么起作用。 –