I am trying to validate authentication in reactive spring application with Spring Security. I could not read content from Mono in the controller. It is not emitting any values when I subscribed. I have the following code in the controller:
@Controller public class TestConroller { public void test(){ Mono<Authentication> monoAuth=ReactiveSecurityContextHolder.getContext().map(SecurityContext::getAuthentication); monoAuth.subscribe(authentication->validate(authentication) } private void validate(Authentication authentication){ System.out.println(authentication.getPrincipal().getName()); } } The validate method is never called
subscribeis not synchronous operation and your request ends before subscription happen. Instead of subscribing explicitly - returnMonofrom your controller