title: How to create a WebClient-object in a spring application with oauth2
body: <p>I'm developing a spring application (client) that is secured with an OAuth2 provider. This application should do some REST calls to another spring application (resource server). For performing the REST calls, I will use spring's WebClient.</p>
  
  <p>I therefore try to create a bean of type WebClient as can be found in several blogs.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class AppConfig {
  
      @Bean
      public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
          ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
               new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations,
                  new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
          oauth.setDefaultClientRegistrationId("myprovider");
          return WebClient.builder().filter(oauth).build();
      }
  }
  </code></pre>
  
  <p>When starting the application, I get the following error:</p>
  
  <pre><code>The following candidates were found but could not be injected:
      - Bean method 'clientRegistrationRepository' in 'ReactiveOAuth2ClientAutoConfiguration' not loaded because NoneNestedConditions 1 matched 0 did not; NestedCondition on ReactiveOAuth2ClientAutoConfiguration.NonServletApplicationCondition.ServletApplicationCondition found 'session' scope
  
  
  Action:
  
  Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' in your configuration.
  </code></pre>
  
  <p>As several websites recommend exactly this code for generating a WebClient instance when using OAuth2 authentication, I'm wondering what I'm doing wrong?</p>
  
  <p>Do you have any suggestions for me?</p>
  
  <p>Thanks.</p>