Expected Behavior
With webflux, @WithMockUser and mockJwt() should have effect if the handler method is annotated with @PreAuthorize. They only have effect if the handler is secured with pathMatchers. It works as expected with mvc and MockMvc
In REST controller :
@GetMapping("foo")
@PreAuthorize("isAuthenticated()")
Mono<String> foo() {
return Mono.just("foo");
}
Failed tests
@Test
@WithMockUser
void testFooWithMockUser() {
webTestClient.get().uri("/foo").exchange().expectStatus().isOk();
}
@Test
void testFooWithMutator() {
webTestClient.mutateWith(mockJwt()).get().uri("/foo").exchange().expectStatus().isOk();
}
Current Behavior
The response status is 401.
Two test cases attached (one with mockmvc and spring-webmvc, one with webTestClient and spring-webflux)
mvc-test-preauthorize.zip webflux-test-preauthorize.zip
Comment From: ah1508
Actually I forgot that @EnableGlobalMethodSecurity must be replaced by @EnableReactiveMethodSecurity for reactive applications.