title: I need to fetch the auth token and set it in the header
body: <p>I'm new to Spring boot and reactive programming.</p>
  
  <p>I'm using spring webflux webclient for an external api service. I need to fetch the auth token and set it in the header</p>
  
  <pre><code>WebClient.builder()
              .baseUrl(baseUrl)
              .filter((request, next) -&gt; {
                  return next.exchange(request)
                          .flatMap((Function&lt;ClientResponse, Mono&lt;ClientResponse&gt;&gt;) clientResponse -&gt; {
                              if (clientResponse.statusCode().value() == 401) {
                                  return authenticate().map(token -&gt; {
                                      Token accessToken = authenticate().block();
                                      ClientRequest retryRequest = ClientRequest.from(request).header("Authorisation", "Bearer " + accessToken.getAccessToken()).build();
                                      return next.exchange(retryRequest);
                                  }).
                              } else {
                                  return Mono.just(clientResponse);
                              }
                          });
              })
              .defaultHeader("Authorization", "Bearer " + authToken.getAccessToken())
              .build();
  
  
  private Mono&lt;Token&gt; authenticate() {
      MultiValueMap&lt;String, String&gt; params = new LinkedMultiValueMap();
      params.add("client_id", clientId);
      params.add("client_secret", clientSecret);
      params.add("grant_type", "password");
      params.add("username", username);
      params.add("password", password);
  
      WebClient client = WebClient.create(baseUrl);
      return client
              .post()
              .uri(tokenUri)
              .accept(MediaType.APPLICATION_JSON)
              .contentType(MediaType.APPLICATION_FORM_URLENCODED)
              .syncBody(params)
              .retrieve()
              .bodyToMono(Token.class);
  }
  
  private static class Token {
      @JsonProperty("access_token")
      private String accessToken;
  
      public String getAccessToken() { return accessToken; }
  }
  </code></pre>
  
  <p>During the application startup, I'll fetch the access token and set it in the webclient builder. I've created a filter to handle authentication failures after token expiry. But the above code throws error because I've used block() which is not supposed to be used in a reactor thread. How else can I handle it? I'm using oauth2 resource owner password grant flow. Is there is any other way to handle the flow?</p>