【问题标题】:Spring OAuth Server JBDCTokenStore ErrorSpring OAuth 服务器 JBDCTokenStore 错误
【发布时间】:2017-11-04 15:06:26
【问题描述】:

我正在尝试将数据库添加到 Spring OAuth Server 示例代码。 这是dataSource配置。

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

和端点配置

    @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 上遇到错误,JdbcClientDetailsS​​ervice 工作正常。

错误信息:

原因:java.lang.IllegalArgumentException:需要数据源

引起:org.springframework.beans.factory.BeanCreationException: 在类路径中定义名称为“tokenStore”的 bean 创建错误 资源 [org/springframework/security/oauth/examples/sparklr/config/OAuth2ServerConfig$AuthorizationServerConfiguration.class]: bean 实例化失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法 [public 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;
    }

我只是在它下面添加一个新的bean并将dataSource更改为dataSource(), 它工作。 我不明白,为什么 JdbcClientDetailsS​​ervice 与原始版本一起使用。 但是 JdbcTokenStore 需要一个新的。 怎么改这部分代码,代码看起来乱七八糟。

【问题讨论】:

  • 你在JdbcTokenStore类上有@Configuration注解吗?
  • 否,但我通过在其下添加重复代码来解决该错误。我不知道它为什么起作用。

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


【解决方案1】:

尝试用@Primary 标记您的数据源bean。在此处查看更多详细信息:77. Data Access

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2012-06-15
    • 1970-01-01
    • 2013-09-05
    • 2021-01-11
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多