I am new to Reactive programming and I have hard time understanding why it appears chaining of Mono does not work in this context. I tried various methods but with no luck. The latest approach is described below where I have written a code for picking up a token from the request header and use it to make a secondary call for token number 2. This token is then used to created an authentication object with given roles.
I tried following in my ServerAuthenticationConverter
implementation:
@Overridepublic @NonNull Mono<Authentication> convert(ServerWebExchange exchange) {
String tokenNr1= extractAuthorizationHeader(exchange);
if (tokenNr1== null || matchBearerLength.test(tokenNr1) == false) {
log.error("Token missing or too short");
return Mono.error(new Exception("aw chucks!"));
}
log.info("Token is " + authorizationHeader);
// return requestTokenNr2 (tokenNr1)<-- return Mono<String> via webcall
return Mono.just("cake")
.map((t) -> {
log.info("Token extracted " + t);
try {
return JWTParser.parse(t);
} catch (ParseException e) {
log.error("Invalid token extracted " + t);
return Mono.error(e);
}
})
.map((jwt) -> createAuthentication((JWT)jwt, extractAuthorizationHeader(exchange)));
}
In this code the mono is obviously subscribed to as I get a parse exception. This is because I use Mono
just with a fixed String. In the line above I have commented out what I was hoping to achieve: In requestTokenNr2
I make a webClient call that ends in bodyToMono
. I expect that when when this converter objects convert method is called both method should at least reach either to the parsing step, or see an error message. The webClient call has a doOnError
call so any exception should have been logged for me to see.
1