My Task is - get JWT token.(all details are here How to get jwt token value in spring webflux? (to exchange it with Minio STS token))
But lets discard redundant details. In an nutshell:
I have a source code:
@GetMapping(..)
public void someEndpoint(...) {
Mono<Object> mono = ReactiveSecurityContextHolder.getContext()
.map(securityContext -> securityContext.getAuthentication().getPrincipal());
mono.block(); //<-- I need to get the result of Mono execution HERE at this thread in a blocking manner
...
}
And I get the error here:
block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-2
Because it is forbidden to use blocking calls in reactor and bla bla bla although in previous versions of reactor this code was working.
I started to looking for a solution of my issue and created 2 topics:
- How to get jwt token value in spring webflux? (to exchange it with Minio STS token)
- How to get raw token from ReactiveSecurityContextHolder?
I've got an advice to make blocking call in a way described here:
So my attempts are:
Attempt 1:
Mono<Object> mono = ReactiveSecurityContextHolder.getContext()
.map(securityContext -> securityContext.getAuthentication().getPrincipal());
Mono<Object> objectMono = mono.subscribeOn(Schedulers.boundedElastic());
Object result = objectMono.block();
Attempt 2:
Mono<Object> mono = ReactiveSecurityContextHolder.getContext()
.map(securityContext -> securityContext.getAuthentication().getPrincipal());
mono.subscribeOn(Schedulers.boundedElastic());
Object result = mono.block();
In both cases I receive the same error:
block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-2
How can I fix it ?
UPDATE 1:
Also I've found the similar topic with the same question but without any answer How do i extract information from Jwt which is stored in ReactiveSecurityContextHolder. It returns a Mono<String> but I need String
UPDATE 2:
All code snippets provided below lead to the same error ( block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-2
)
A)
Mono.just("qwerty")
.subscribeOn(Schedulers.boundedElastic())
.publishOn(Schedulers.boundedElastic())
.block()
B)
Mono<String> customMono = Mono.just("qwerty");
Mono<String> blockedMono = customMono
.subscribeOn(Schedulers.boundedElastic())
.publishOn(Schedulers.boundedElastic());
System.out.println("blockedMono.block(): " + blockedMono.block());
C)
Mono<String> customMono = Mono.just("qwerty");
Mono<String> blockedMono = Mono.just(0)
.subscribeOn(Schedulers.boundedElastic())
.publishOn(Schedulers.boundedElastic())
.then(customMono);
System.out.println("blockedMono.block(): " + blockedMono.block());