Spring Security 5.2.x的OAuth 2.0客户端功能不支持RestTemplate,仅支持WebClient。参见Spring Security Reference:
  HTTP客户端支持
  
  
  WebClient Servlet环境的集成(用于请求受保护的资源) 
  
此外,RestTemplate在以后的版本中将不推荐使用。参见RestTemplate javadoc:
  注意:从5.0开始,无阻塞,无功
   org.springframework.web.reactive.client.WebClient提供了的现代替代方案,对RestTemplate同步和异步以及流方案均提供了有效的支持。该RestTemplate会在未来的版本中被淘汰,并没有重大的新功能的加入前进。有关WebClient更多详细信息和示例代码,请参见Spring Framework参考文档的部分。
因此,最好的办法是放弃RestTemplate赞成WebClient。
使用WebClient的客户端凭证流
通过编程或使用Spring Boot自动配置来配置客户端注册和提供程序:
spring:
  security:
    oauth2:
      client:
        registration:
          custom:
            client-id: clientId
            client-secret: clientSecret
            authorization-grant-type: client_credentials
        provider:
          custom:
            token-uri: http://localhost:8081/oauth/token
......和OAuth2AuthorizedClientManager @Bean:
@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;
}
配置WebClient实例以ServerOAuth2AuthorizedClientExchangeFilterFunction与提供的实例一起使用OAuth2AuthorizedClientManager:
@Bean
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
            new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("custom");
    return WebClient.builder()
            .apply(oauth2Client.oauth2Configuration())
            .build();
}
现在,如果您尝试使用此WebClient实例发出请求,它将首先从授权服务器请求令牌,并将其包含在请求中。
               
              
WebClient)或类似的东西从中获取OAuth令牌自定义OAuth提供程序(不是受支持的OoTB之一,如Facebook / Google),以便将其添加到外发请求中。所有这些示例似乎都集中在与其他提供者之间对传入请求进行身份验证。你有任何好的例子的指针吗?