spring-security: Test using @WithMockUser fails with 401 UNAUTHORIZED with 3.2
Describe the bug The following test works with 3.1, but fails with 401 UNAUTHORIZED with 3.2:
To Reproduce
@SpringBootTest(
webEnvironment = RANDOM_PORT,
classes = [Application::class],
properties = ["spring.main.allow-bean-definition-overriding=true"]
)
@TestConstructor(autowireMode = ALL)
@WithMockUser(authorities = [ROLE_USER])
class HandlerIntegrationTest(
@MockkBean
private val searcher: Searcher,
private val client: WebTestClient,
) {
@Test
fun search() {
val searchResponse = SearchResponse(
result = listOf(),
page = 1,
pageSize = 20,
total = 0,
filterOptions = FilterOptions(setOf(), setOf()),
sort = setOf(),
descending = listOf(false),
)
coEvery { searcher.search(any()) } returns searchResponse
client.get()
.uri { it.path("$ENDPOINT/search").build() }
.exchange()
.expectStatus()
.isOk
.expectBody<SearchResponse>()
.consumeWith {
assertThat(it.responseBody).isEqualTo(searchResponse)
}
}
}
A custom MapReactiveUserDetailsService has been in place and it looks like this:
@Bean
@ConditionalOnProperty(value = ["spring.security.user.passwordGenerated"], matchIfMissing = true, havingValue = "false")
fun userDetailsService(): MapReactiveUserDetailsService {
val actuatorUser = User
.withUsername(securityProperties.user.name)
.password("{noop}${securityProperties.user.password}")
.authorities(AUTHORITY_ACTUATOR, AUTHORITY_ACCESS_MONITORING, AUTHORITY_ACCESS_INTERNAL_API).build()
val monitoringUser = User
.withUsername(monitoringProperties.username)
.password("{noop}${monitoringProperties.password}")
.authorities(AUTHORITY_ACCESS_MONITORING)
.build()
return MapReactiveUserDetailsService(actuatorUser, monitoringUser)
}
Expected behavior The test works
Sample Currently we have tried in 3 different projects with different services always with this same result, thus this might not be an isolated case, but rather a major issue, thus hopefully you can easily identify the cause.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 17 (8 by maintainers)
Commits related to this issue
- Restore where onLastOperatorHook is applied Recent improvements in the automatic context propagation (#3549) resulted in a regression – some sites where the "last operator" hook was previously applie... — committed to reactor/reactor-core by chemicL 3 years ago
- Restore where onLastOperatorHook is applied (#3673) Recent improvements in the automatic context propagation (#3549) resulted in a regression – some sites where the "last operator" hook was previou... — committed to reactor/reactor-core by chemicL 3 years ago
Thanks for the sample @andrashatvani. It took me a while to make it minimal and runnable, but I found some clues about the problem. I changed my sample to reproduce the error.
The error is only triggered when we do
http.csrf(ServerHttpSecurity.CsrfSpec::disable), if you comment that line the test works. I am not sure if this is a problem in Spring Security yet because if using Spring Boot 3.1.6 and Spring Security 6.2.0, the problem does not happen. I’ll keep you updated with my findings.Hi, folks. Unfortunately, I haven’t found the root cause yet, however, I can provide a simple workaround so you can update your applications:
Some newest finding points out that there is something related to the Reactor Core and I’ll talk with someone from their team early next week.
Closed via https://github.com/reactor/reactor-core/pull/3673
Hi, everyone. Thanks to the help of @chemicL we managed identify a regression that happened in reactor-core
3.6.0. The PR with the fix is available here.As soon as reactor core
3.6.2is available, Spring Security will update to it and I may close this issue if the fix is confirmed. Spring Security6.2.2is scheduled for Feb 19th, however, when reactor-core is out you can override its dependency version if you want.The issue is caused by a regression introduced in reactor-core 3.6.0 and the PR linked above should bring back the previous behaviour.
In the PR I expressed an opinion that the
onLastOperatorHookfunctionality should probably be deprecated. From my understanding, it was added to improveThreadLocalpropagation acrossThreadboundaries. It might have been added specifically for Spring Cloud Sleuth which performs tracing instrumentation. onLastOperatorHook has less execution points than theonEachOperatorHookto reduce the performance impact, but requires additional hooks to improve the reliability (onScheduleHook+ queue wrapper hook) and still has its shortcomings. Starting from reactor-core 3.5.0 we introduced better mechanisms for propagation ofThreadLocalstate.The issue with
onLastOperatorHookcan be understood better with a blog post series we published on the subject – part 2 discusses this hook.The latest-and-greatest context propagation features are described in our documentation.
Hi, everyone. I’ve created a minimal sample but I had no success reproducing the problem. Can someone apply the needed changes to make the problem reproducible?