1

I am using Javers and need to provide the author of a commit. In order to do this, I create this class

  private class SimpleAuthorProvider : AuthorProvider {

    override fun provide(): String {
        return "the_author"
    }
}

However instead of hard coding the string, I would like to use the user who has sent the request. I am using a ReactiveAuthenticationManager to set the authentication. This means I need to access it using ReactiveSecurityContextHolder.getContext() which returns a Mono. As I need to essentially return a string from this, I am not sure how to access it?

I have tried to even do a hack or two e.g. block() or toFuture().get() but these both do not work.

Are there any other suggestions?

Niamh
  • 31
  • 3

1 Answers1

0

First, if you are new to reactive programing, don't use Webflux. It's a very complex framework, which you probably don't need -- see this talk https://www.youtube.com/watch?v=5TJiTSWktLU

There is no out-of-the-box integration for Javers and Webflux. Still, Javers supports simple async programing based on CompletableFutures from Java lang. What you can do is to convert CompletableFuture returned by javers.commitAsync() to Mono which will made Webflux happy.

As a rule of thumb, never call block() on Mono in production code. You should register an action -- here javers.commitAsync() -- which will be executed when a Mono is completed using doOnSuccess() or map(), see https://www.naturalprogrammer.com/blog/16393/spring-security-get-current-user-programmatically.

Bartek Walacik
  • 3,386
  • 1
  • 9
  • 14
  • Thanks for your advice and help (and apologies for the late response). We've managed to get a work around done (and fwiw, I didn't decide on the reactive framework, and we intend to move away from it). – Niamh Jan 08 '21 at 16:23