如何在@SpringBootTest中加载ServerHttpSecurity?
我想创建一个使用我的完整配置结构的 @SpringBootTest
。
问题:我正在创建一个需要 ServerHttpSecurity
的 @Bean SecurityWebFilterChain
,但在测试中不知何故缺少:
@SpringBootApplication
public class MainApp { ... }
//for simple testing I started with anonymous auth
@Configuration
public class ReactiveSecurityConfiguration {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.anonymous()
.and().csrf().disable()
.build();
}
}
@SpringBootTest
public class TestClass {
@Test
public void test() {
}
}
结果:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'securityWebFilterChain' defined in class path resource [ReactiveSecurityConfiguration.class]:
Unsatisfied dependency expressed through method 'securityWebFilterChain' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.security.config.web.server.ServerHttpSecurity' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
更新
我发现如果我将以下注释添加到我的 Test 类中,测试方法工作正常,没有错误。但这是故意的吗?
@EnableWebFlux
@EnableWebFluxSecurity
@SpringBootTest
public class TestClass { }
I want to create a @SpringBootTest
that makes use of my full configuration structure.
Problem: I'm creating an @Bean SecurityWebFilterChain
that requires a ServerHttpSecurity
, which is somehow missing in a test:
@SpringBootApplication
public class MainApp { ... }
//for simple testing I started with anonymous auth
@Configuration
public class ReactiveSecurityConfiguration {
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
return http.anonymous()
.and().csrf().disable()
.build();
}
}
@SpringBootTest
public class TestClass {
@Test
public void test() {
}
}
Result:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'securityWebFilterChain' defined in class path resource [ReactiveSecurityConfiguration.class]:
Unsatisfied dependency expressed through method 'securityWebFilterChain' parameter 0;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.security.config.web.server.ServerHttpSecurity' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Update
I discovered if I add the following annotations to my Test Class, the testmethod works without errors. But is that intentional?
@EnableWebFlux
@EnableWebFluxSecurity
@SpringBootTest
public class TestClass { }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在类路径中的某处拉入了 spring-boot-starter-web 依赖项。删除它解决了问题。
如果应保留 web 和 webflux 依赖项,则仍然可以仅以响应方式运行测试,方法是:
I have a
spring-boot-starter-web
dependency pulled somewhere in the classpath. Removing it resolved the problem.If both web and webflux dependency should be kept, it's still possible to run a test in reactive only, with:
我不确定你的项目是如何安排的,但是不,这不是故意的。您可以查看 Spring Security 的响应式示例 演示了这一点。
您的
TestClass
可能找不到附加到MainApp
的@SpringBootConfiguration
注释。I'm not certain how your project is arranged, but no, it's not intentional. You can take a look at Spring Security's Reactive Sample that demonstrates this.
It may be that your
TestClass
is not finding the@SpringBootConfiguration
annotation attached toMainApp
.