Spring boot 弹簧安全Oauth2测试-表;OAUTH“访问令牌”;找不到

Spring boot 弹簧安全Oauth2测试-表;OAUTH“访问令牌”;找不到,spring-boot,h2,spring-security-oauth2,Spring Boot,H2,Spring Security Oauth2,这里我用的是InMemoryTokenStore。运行应用程序时,我可以通过执行以下操作使此TokenStore正常工作: @Configuration public class AppConfig { @Bean public TokenStore tokenStore() { return new InMemoryTokenStore(); } // ... } 现在我可以向/oauth/token发出HTTP请求并获取访问令牌。我还可以

这里我用的是InMemoryTokenStore。运行应用程序时,我可以通过执行以下操作使此TokenStore正常工作:

@Configuration
public class AppConfig {

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    // ...
}
现在我可以向/oauth/token发出HTTP请求并获取访问令牌。我还可以访问授权资源。而且,我的测试甚至在运行中传递和创建表(例如oauth_access_token table)

但是,我不想在开发环境和以后的生产环境中使用InMemoryTokenStore。因此,我尝试在测试属性文件中使用configureh2,仅用于我的测试,其他环境我打算使用MySQL

所以我修改了我的配置类:

@Configuration
public class AppConfig {

    @Value("${spring.datasource.url}")
    private String datasourceUrl;

    @Value("${spring.datasource.username}")
    private String dbUsername;

    @Value("${spring.datasource.password}")
    private String dbPassword;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(datasourceUrl);
        dataSource.setUsername(dbUsername);
        dataSource.setPassword(dbPassword);
        return dataSource;
    }

    @Bean
    public TokenStore tokenStore(DataSource dataSource) {
        return new JdbcTokenStore(dataSource);
    }

    // ...
}
对于每个环境,我有两个application.properties文件:

src/main/resources/application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:33067/budget?useSSL=false
spring.datasource.username=budget
spring.datasource.password=s3cret
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
src/test/resources/application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:33067/budget?useSSL=false
spring.datasource.username=budget
spring.datasource.password=s3cret
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
我的意图是,测试环境将选择H2 db配置,其他环境将使用MySQL db配置。但是,我现在在测试中看到以下错误:

2019-12-30 21:09:41.627 ERROR 20628 --- [           main] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: BadSqlGrammarException, PreparedStatementCallback; bad SQL grammar [select token_id, token from oauth_access_token where authentication_id = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Table "OAUTH_ACCESS_TOKEN" not found; SQL statement:
select token_id, token from oauth_access_token where authentication_id = ? [42102-197]

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select token_id, token from oauth_access_token where authentication_id = ?]; nested exception is org.h2.jdbc.JdbcSQLException: Table "OAUTH_ACCESS_TOKEN" not found; SQL statement:
select token_id, token from oauth_access_token where authentication_id = ? [42102-197]
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
...

我可以确认测试正在选择H2配置,但由于缺少表,因此会抛出一个错误。如果我跳过测试,那么我可以运行应用程序并成功发出HTTP请求——如果它创建的MySQL数据库中缺少该表。InMemoryTokenStore配置也会创建表,因此不会发生此错误-但我不希望dev/prod出现此错误。但是测试期间的H2配置(src/test/resources/application.properties)会抛出一个错误,因为缺少表,所以它似乎不会创建任何表。我做错了什么?

我不确定,但尝试将spring.jpa.hibernate.ddl auto=create添加到您的测试应用程序.properties中。因为据我所知,H2不仅仅是在启动时创建表。或者我发现的其他东西,就是在你的resources文件夹中创建一个.sql文件,它会为你创建表,h2会自动检测到。实际上,我可能弄错了。@Entity表似乎是在不存在的情况下创建的,但我可能已经手动创建了这些额外的令牌表。我刚刚遇到一个我几周前做的教程,其中包括一些include语句。无论如何,现在我已经在MySQL中创建了一个测试数据库,并手动创建了那些令牌表,现在我的测试正在针对它运行。现在已经足够好了。