2.1 Release Notes 中的 SpringBoot 包含以下信息:
Security configuration is now applied to WebTestClient. For more information on testing secured endpoints, please refer to the relevant section of Spring Security’s reference documentation.
问题:
将 SpringBoot 从 2.0.4 更新到 2.1.2 后,我发现我的测试已经停止工作。我正在使用 @SpringBootTest
进行 REST 测试。我的 WebTestClient
无法连接到服务器。我确实尝试了很多(例如来自 here ) 模拟或禁用安全性并仍然得到 403 FORBIDDEN
响应。
你有什么线索可以出错吗?
我按以下方式创建 WebTestClient
:
client = WebTestClient
.bindToServer()
.baseUrl("http://localhost:$port")
.build()
还尝试排除 SecurityAutoConfiguration.class
。
请您参考如下方法:
在某个黑暗的地方,在兔子洞的深处,我发现了这个:
@TestConfiguration
@Order(1)
public class SecurityConfiguration
implements WebSecurityConfigurer<WebSecurity> {
@Override
public void init(WebSecurity builder) throws Exception {
builder.ignoring().requestMatchers(
new AntPathRequestMatcher("/**"));
}
@Override
public void configure(WebSecurity builder) throws Exception {
}
}
记得在@SpringBootTest
中注册类,例如:
@SpringBootTest(
classes = [SomeApplication, SecurityConfiguration],
webEnvironment = RANDOM_PORT
)
它并没有禁用 spring security,而是让它变得透明。