title: Spring Security OAuth2 client_credentials + Webclient without webserver
body: <p>I have an application, that currently uses RestTemplate + OAuth2. The application itself is NOT a Spring MVC app, so for example no ports are open (no @GetMapping what so ever).</p>
  <p>I am migrating from Spring Security OAuth 2.x to Spring Security 5.2.x. using this guide:
  <a href="https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide" rel="nofollow noreferrer">https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide</a></p>
  <p>Config is:</p>
  <pre><code>@Configuration
  public class WebClientConfig {
      @Bean
      public OAuth2AuthorizedClientManager authorizedClientManager(
              ClientRegistrationRepository clientRegistrationRepository,
              OAuth2AuthorizedClientRepository authorizedClientRepository) {
  
          OAuth2AuthorizedClientProvider authorizedClientProvider =
                  OAuth2AuthorizedClientProviderBuilder.builder()
                          .clientCredentials()
                          .build();
  
          DefaultOAuth2AuthorizedClientManager authorizedClientManager =
                  new DefaultOAuth2AuthorizedClientManager(
                          clientRegistrationRepository, authorizedClientRepository);
          authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
  
          return authorizedClientManager;
      }
  
      @Bean
      public WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
          ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
                  new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
          oauth2Client.setDefaultClientRegistrationId(&quot;regid&quot;);
          return WebClient.builder()
                  .apply(oauth2Client.oauth2Configuration())
                  .build();
      }
  }
  </code></pre>
  <p>application.properties:</p>
  <pre><code>spring.security.oauth2.client.registration.regid.provider=regid
  spring.security.oauth2.client.registration.regid.client-id=java-web
  spring.security.oauth2.client.registration.regid.client-secret=secret
  spring.security.oauth2.client.registration.regid.authorization-grant-type=client_credentials
  spring.security.oauth2.client.provider.regid.token-uri=https://...token
  spring.security.oauth2.client.provider.regid.jwk-set-uri=https://..certs
  </code></pre>
  <p>The problem is:
  This app runs on a server that has port 8080 already allocated.
  Previous version (Spring Oauth2 + RestTemplate) did NOT open any ports, especially 8080.</p>
  <p>It seems that I can not use WebClient without making this app a webapp, listening on port 8080, which I cannot open, and I don't want to change to a random port to avoid collision. The app just simply does not need any ports to be opened.</p>
  <p>I've tried several things, for example:</p>
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;exclusions&gt;
          &lt;exclusion&gt;
              &lt;artifactId&gt;tomcat-embed-el&lt;/artifactId&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
          &lt;/exclusion&gt;
          &lt;exclusion&gt;
              &lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
          &lt;/exclusion&gt;
          &lt;exclusion&gt;
              &lt;artifactId&gt;tomcat-embed-websocket&lt;/artifactId&gt;
              &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
          &lt;/exclusion&gt;
      &lt;/exclusions&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>And also:</p>
  <pre><code>spring.main.web-application-type=none
  </code></pre>
  <p>etc., but every single time I've encountered a <em>&quot;random&quot;</em> NoClassDefFound javax.servlet.*, or: &quot;Could not autowire. No beans of 'x' type found.&quot;</p>
  <p>How can I setup the app:</p>
  <ul>
  <li>using newest Spring Security with OAuth2</li>
  <li>using WebClient</li>
  <li>not being a webapp itself
  ?</li>
  </ul>
  <p>thank you</p>