Summary

When attempting to retrieve the Principal in either a WebFlux Controller or Handler method (using the getPrincipal method) a null value is always returned, even when there is an authenticated user. This was working fine in 5.0.0.M5 but seems to have stopped working some time after so it currently affects 5.0.0.RC1 & BUILD-SNAPSHOT.

Actual Behavior

A null value is always returned for the Principal when using the getPrincipal method on either the ServerWebExchange or ServerRequest in a WebFlux Controller or Handler method even when there is an authenticated user.

Expected Behavior

A Principal object should be returned as it was in 5.0.0.M5 if there is an authenticated user.

Configuration

Default Spring Security & Boot auto configuration.

Version

Spring Security 5.0.0.RC1 & BUILD-SNAPSHOT Spring Boot 2.0.0.BUILD-SNAPSHOT

Comment From: rwinch

Thanks for the report @essh! I'm having difficulty reproducing the problem. All of the following work for me:

@GetMapping("/principal")
public String principal(Principal principal) {
    return "Hello " + principal.getName();
}

@GetMapping("/principal/mono")
public Mono<String> principalMono(Mono<Principal> principal) {
    return principal.map( p -> "Hello " + p.getName());
}

@GetMapping("/userdetails")
public String principal(@AuthenticationPrincipal UserDetails userDetails) {
    return "Hello " + userDetails.getName();
}

@GetMapping("/userdetails/mono")
public Mono<String> userDetailsMono(@AuthenticationPrincipal Mono<UserDetails> userDetails) {
    return userDetails.map( p -> "Hello " + p.getName());
}

@GetMapping("/exchange/principal")
public Mono<String> exchangePrincipal(ServerWebExchange exchange) {
    return exchange.getPrincipal().map( p -> "Hello " + p.getName());
}

You can find a working sample at https://github.com/rwinch/spring-security-sample/tree/spring-security-gh-4790

Can you provide me more details so that I can reproduce the problem?

Comment From: essh

Thanks for the demo. It looks like I messed up my handling of the reactor chain and I managed to get it to work now.

Comment From: rwinch

@essh No problem thanks for the quick response!

Comment From: kaivanshah14

@rwinch I tried returning the principal using ServerWebExchange Decorator, but it is giving "MonoMapFusable" instead of the principal, I am trying to do an MDC put where I want to log the principal with other MDC contents (We are using spring reactive and a web filter for the purpose)