Java 使用Spring引导和OAuth2实现JdbcTokenStore
嘿 我正在尝试使用Spring Boot在应用程序中实现OAuth2。我正在努力实现JdbcTokenStore(我是否正确理解这一点,这是为了将令牌存储在数据库中?) 我的代码如下所示:Java 使用Spring引导和OAuth2实现JdbcTokenStore,java,spring,spring-security,oauth-2.0,spring-boot,Java,Spring,Spring Security,Oauth 2.0,Spring Boot,嘿 我正在尝试使用Spring Boot在应用程序中实现OAuth2。我正在努力实现JdbcTokenStore(我是否正确理解这一点,这是为了将令牌存储在数据库中?) 我的代码如下所示: @Configuration @EnableAuthorizationServer protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { @Autowired private Da
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private DataSource dataSource;
private TokenStore tokenStore = new JdbcTokenStore(dataSource);
执行此操作时,我会遇到以下错误:
由以下原因引起:org.springframework.beans.BeanInstantiationException:无法实例化bean类[se..config.OAuth2ServerConfig$OAuth2Config$$EnhancerBySpringCGLIB$$f6b9ba94]:构造函数引发异常;嵌套异常为java.lang.IllegalArgumentException:需要数据源
数据库连接本身似乎工作正常,至少当我使用InMemoryTokenStore时,我可以使用存储在数据库中的用户数据(用户名/密码)登录
有谁能告诉我我做错了什么,或者推荐一些关于这个问题的好例子
非常感谢 这是一个基本的依赖注入问题(没有花哨的东西,也没有oauth或引导相关的东西)。无法从
@Autowired
初始化字段(初始化后进行连接)。您需要将其拉入@PostConstruct
或@Bean
另一个配置类中将dataSource()定义为@Bean。我想我可以在以后使用@Autowired?访问它,您可以,但您不能在字段初始值设定项中使用它。将令牌存储创建拉到方法中很简单。请参阅
@Override
public void configure(OAuth2AuthorizationServerConfigurer oauthServer) throws Exception {
oauthServer.tokenStore(tokenStore).authenticationManager(authenticationManager);
}