Search code examples
javaspringspring-cloudspring-cloud-config

What is the right approach for extending Spring Cloud Config Client?


I want to replace Basic Authentication for Spring Cloud Config Server with oAuth implementation. Let's leave Config Server alone for now and focus on changes for Config Client. Obviously I don't want to write my own implementation for whole thing, but instead execute my own logic and fallback on standard Config Client. Also I have to pack my changes into library since I will use it in multiple micro-services.

Long story short I want to achieve following:

1a. Create custom Starter which will contain Spring Cloud Config Client as dependency. Is it even doable or necessary?

or

1b. Create custom Starter with only my custom logic which will be executed before Spring Cloud Config Client. In this case each micro-service will have Spring Cloud Config Client and custom Starter as dependencies. How can I manage execution order and inject custom logic results into Config Client?

2.Introduce new bootstrap settings. e.g. spring.cloud.config.custom.username and spring.cloud.config.custom.password (Optional).

3.Introduce custom annotation for custom Starter. e.g. @enableCustomConfigClient (Optional).

I started with building custom Starter with following code in /resources/META-INF/spring.factories:

# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.greeter.config.ConfigClientBootstrapConfiguration

But this code invoked after profile is set, not the first thing like Config Client does.

Any suggestions and especially code samples are appreciated. Thanks!


Solution

  • Posting approach I chose for future reference.

    1. Create new package which will be executed on top of / before Spring Cloud Config Client. Two main features here:

      • Create file src/main/resources/META-INF/spring.factories with org.springframework.cloud.bootstrap.BootstrapConfiguration={YOUR_CLASS}

      • In {YOUR_CLASS} apply custom logic. Don't forget to use @org.springframework.core.annotation.Order({YOUR_PRECEDENCE}) and fact that Ordered.LOWEST_PRECEDENCE will be executed first

    2. Build jar from previous step and include it into your project (as local file or via artifactory)

    3. Add Custom logic to Spring Cloud Config Server so it can use JWT.

    Working example is here: https://github.com/ka4ok85/spring-cloud-config-client-jwt