【问题标题】:JdbcTokenStore implementation with Spring-Boot and OAuth2使用 Spring-Boot 和 OAuth2 实现 JdbcTokenStore
【发布时间】:2014-05-11 06:11:30
【问题描述】:

嘿嘿,

我正在尝试使用 Spring-Boot 在应用程序中实现 OAuth2。我正在努力实现 JdbcTokenStore(我是否理解正确,这是将令牌存储在数据库中?)

我的代码如下所示:

@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    private TokenStore tokenStore = new JdbcTokenStore(dataSource);

....

    @Override
    public void configure(OAuth2AuthorizationServerConfigurer oauthServer) throws Exception {
         oauthServer.tokenStore(tokenStore).authenticationManager(authenticationManager);
    }

这样做我得到以下错误:

原因:org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [se.....config.OAuth2ServerConfig$OAuth2Config$$EnhancerBySpringCGLIB$$f6b9ba94]:构造函数抛出异常;嵌套异常是 java.lang.IllegalArgumentException: DataSource required

数据库连接本身似乎可以工作,至少我在使用InMemoryTokenStore时可以使用存储在数据库中的用户数据(用户名/密码)登录。

谁能告诉我我做错了什么或推荐来源,其中有很好的例子来解决这个问题?

非常感谢!

【问题讨论】:

标签: java spring spring-security oauth-2.0 spring-boot


【解决方案1】:

这是一个基本的依赖注入问题(没什么花哨的,也没什么 oauth 或 Boot 相关的)。您不能从@Autowired 初始化字段(在初始化之后进行接线)。您需要将其提取到@PostConstruct@Bean

【讨论】:

  • dataSource() 在另一个配置类中定义为@Bean。我以为以后可以使用@Autowired 访问它?
  • 你可以,但你不能在字段初始化器中使用它。将令牌存储创建拉到一个方法中很简单。
猜你喜欢
  • 2020-06-11
  • 2018-11-21
  • 2017-04-20
  • 2015-09-20
  • 2019-01-09
  • 2018-01-05
  • 2017-01-19
  • 2020-06-19
  • 2017-05-13
相关资源
最近更新 更多