我有一个带有数据库和 rabbitmq 用法的小型 Spring Boot 应用程序。所以我想用集成测试(H2 + apache qpid)进行测试。
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestSpringConfig.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Run Code Online (Sandbox Code Playgroud)
正如我的应用程序所期望的那样,数据库和 mq 我使用 @BeforeAll 来启动它:
@BeforeAll
public void before() {
startMessageBroker();
startDatabase();
}
Run Code Online (Sandbox Code Playgroud)
问题是我的 Web 应用程序在 @BeforeAll 中定义的 database/mq 之前启动。
org.springframework.test.context.junit.jupiter.SpringExtension:
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor,
BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback,
ParameterResolver {
// ...
@Override
public void beforeAll(ExtensionContext context) throws Exception {
getTestContextManager(context).beforeTestClass();
}
// ...
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
getTestContextManager(context).prepareTestInstance(testInstance);
}
// ...
Run Code Online (Sandbox Code Playgroud)
Web …
我正在尝试将Spring OAuth2用于我的应用程序.但看起来我犯了一个错误,我可以找到我做的地方.流程应该是:1.使用用户名和密码从/ oauth/token获取令牌2.使用提供的令牌向/ security发出请求
MethodSecurityConfig:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Autowired
private SecurityConfiguration securityConfig;
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
OAuth2ServerConfig:
@Configuration
public class OAuth2ServerConfig {
private static final String RESOURCE_ID = "nessnity";
@Configuration
@Order(10)
protected static class UiResourceConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers().antMatchers("/security")
.and()
.authorizeRequests()
.antMatchers("/security").access("hasRole('USER')");
}
}
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public …Run Code Online (Sandbox Code Playgroud)