0
votes

I'm using spring boot 2.3.6.RELEASE version. I need to configure my application for oauth2 client credentials with dynamic configuration with webflux integration.

I have tried following code.

@Configuration
public class Oauth2ClientConfig {

    @Bean
    ReactiveClientRegistrationRepository getRegistration() {
        ClientRegistration registration = ClientRegistration
                .withRegistrationId("custom")
                .tokenUri(env.getProperty("accessTokenUri"))
                .clientId(env.getProperty("clientID"))
                .clientSecret(env.getProperty("clientSecret"))
                .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                .scope(env.getProperty("scope"))
                .build();
        return new InMemoryReactiveClientRegistrationRepository(registration);
    }

    @Bean(name = "custom")
    WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
                clientRegistrations, new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
        oauth.setDefaultClientRegistrationId("custom");
        return WebClient.builder()
                .filter(oauth)
                .build();
    }
}

but in this spring boot version UnAuthenticatedServerOAuth2AuthorizedClientRepository is depricated. as per the spring documentation it says to use AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager insted of UnAuthenticatedServerOAuth2AuthorizedClientRepository but I couldn't find any proper sample for this implementation. If anyone has an idea about how to implement this configuration please help.

1

1 Answers

1
votes

I found the solution my own for depricated UnAuthenticatedServerOAuth2AuthorizedClientRepository.

Spring doc says to use AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager insted of UnAuthenticatedServerOAuth2AuthorizedClientRepository. you can find it here.

here is the complete sample for configuring webflux with Oauth2 in latest spring boot version.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.oauth2.client.AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.InMemoryReactiveOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.InMemoryReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class Oauth2WebClientConfig {

    private final Environment env;

    @Autowired
    public Oauth2WebClientConfig(Environment env) {
        this.env = env;
    }

    // == Oauth2 Configuration ==
    @Bean
    ReactiveClientRegistrationRepository clientRegistration() {
        ClientRegistration clientRegistration = ClientRegistration
                .withRegistrationId("custom")
                .tokenUri("tokenUri")
                .clientId("clientId")
                .clientSecret("clientSecret")
                .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                .scope("scope")
                .build();
        return new InMemoryReactiveClientRegistrationRepository(clientRegistration);
    }

    @Bean
    ReactiveOAuth2AuthorizedClientService authorizedClientService() {
        return new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistration());
    }
    // == Oauth2 Configuration ==

    // == WebFlux Configuration ==
    @Bean
    WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations, ReactiveOAuth2AuthorizedClientService authorizedClientService) {
        ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
                new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations, authorizedClientService));
        oauth.setDefaultClientRegistrationId("custom");
        return WebClient.builder()
                .filter(oauth)
                .build();
    }


    // == WebFlux Configuration ==
}