Spring Boot WebClient - Token endpoint protected with username and password

Viewed 17

I have to make calls against OAuth2 protected endpoints. To achieve this i'm using a Spring Boot WebClient (See a bit under to check how i'm creating the beans). But i have the following problem:

On a common schema one calls the token endpoint and pass grant_type, client_id and client_secret as request body.

In the schema that i'm using, i have to pass client_id and secret on the Authorization header.

The following Postman requests works

Request Body

Request Authorization

Is there a way to configure my ClientRegistration, so that it sets the Authorization Header?

WebClientFactory:

public class WebClientFactory {

public WebClient webClient() {
    var oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
        getRegistrationRepository(),
        new UnAuthenticatedServerOAuth2AuthorizedClientRepository()
    );

    oauth.setDefaultClientRegistrationId("id");
    return WebClient.builder()
        .filter(oauth)
        .build();
}

private ReactiveClientRegistrationRepository getRegistrationRepository(){
    var tokenUri = "Token injected from env";
    var clientId = "Client id injected from env";
    var clientSecret = "Client secret injected from env";

    var registration = ClientRegistration
        .withRegistrationId("id")
        .tokenUri(tokenUri)
        .clientId(clientId)
        .clientSecret(clientSecret)
        .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
        .build();
    
    return new InMemoryReactiveClientRegistrationRepository(registration);
}

}

PS: I know that i'm using some deprecated classes, but i can't upgrade the versions.

0 Answers
Related