I'm trying to create a Spring Boot REST application that has to make a remote REST call to another Spring Boot application protected by OAuth2 (with grant type client_credentials).
The first application is using the Reactive WebClient to make the call to the second OAuth2 REST application.
I've configured the the WebClient with grant_type "client_credentials" (see code below)
public WebClient messageWebClient(
ClientRegistrationRepository clientRegistrations,
OAuth2AuthorizedClientRepository authorizedClients,
ClientHttpConnector clientHttpConnector
) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
oauth.setDefaultClientRegistrationId("message");
return WebClient.builder()
.baseUrl(MESSAGE_BASE_URL)
.clientConnector(clientHttpConnector)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.filter(oauth)
.filter(logRequest())
.build();
}
@Bean
public ClientRegistrationRepository clientRegistrations() {
ClientRegistration clientRegistration = ClientRegistration
.withRegistrationId("message")
.clientId("client")
.clientSecret("secret")
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.tokenUri("http://localhost:8081/oauth/token")
.build();
return new InMemoryClientRegistrationRepository(clientRegistration);
}
But every time I'm making a Postman call to the first application I end up with the following exception:
"IllegalArgumentException: Invalid Authorization Grant Type (client_credentials) for Client Registration with Id: ..." from the DefaultOAuth2AuthorizationRequestResolver
Is "client_credentials" really supported for WebClient... or am I missing something ?
Regards
- First Application code: https://github.com/fdlessard/SpringBootOauth2WebClient
SecondApplication code: https://github.com/fdlessard/SpringBootOAuth2Message
SpringBoot Version 2.1.4
- spring-security-oauth2-client: 5.1.5.RELEASE

.apply(oauth2.oauth2Configuration())used instead of.filter(oauth). Why do you is it in another way? - dur