如何在@SpringBootTest中加载ServerHttpSecurity?

发布于 2025-01-11 05:57:54 字数 1458 浏览 0 评论 0原文

我想创建一个使用我的完整配置结构的 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2025-01-18 05:57:54

我在类路径中的某处拉入了 spring-boot-starter-web 依赖项。删除它解决了问题。

如果应保留 web 和 webflux 依赖项,则仍然可以仅以响应方式运行测试,方法是:

@SpringBootTest(properties = "spring.main.web-application-type=REACTIVE")
class MyWebFluxTests {

    // ...

}

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:

@SpringBootTest(properties = "spring.main.web-application-type=REACTIVE")
class MyWebFluxTests {

    // ...

}
擦肩而过的背影 2025-01-18 05:57:54

我不确定你的项目是如何安排的,但是不,这不是故意的。您可以查看 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 to MainApp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文