Feign Clients: Handling Multiple Authorization Approach

In a typical application platform of any size, communication is either synchronous or asynchronous. For synchronous communication that is HTTP-based, there exist, numerous clients, to choose from. However, for the Java ecosystem, feign (https://github.com/OpenFeign/feign)  provides an abstraction over this and offers among other benefits: multiple encoding, better error handling, logging, etc.

Feign offers a declarative way of making HTTP calls in Java and Spring Boot supports it as part of its Spring could project (https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/)

Scenario

In a situation where you have a set of feign clients to make calls to internal services – this means they arguably use the same authorization approach; we can tag this set as the primary ones since they will use the same configuration. We can tag the other (set of) feign clients used to communicate with third-party providers with a different authorization approach as the secondary client.

Together with spring security, it is possible to enable these clients for autoconfiguration in such a way that requests through the clients are intercepted and wrapped with the preferred authorization approach e.g., oauth2. This leads to the question of how to exclude the secondary client from using the (auto) configuration of the primary one.

Solution

The logical way to go about this is to have a different configuration class for the to-be-excluded feign client and inside this class, add a bean to disable inheriting configuration from the parent one. If the authorization approach of the secondary client is not directly inclusive (not part of the headers/parameter), you might need to add a request interceptor bean in the config class.

       public class CustomSecondaryFeignClientConfiguration{

            @Bean
             public FeignClientConfigurer feignClientConfigurer() {
                  return new FeignClientConfigurer() {
                        @Override
                       public boolean inheritParentConfiguration() {
                         return false;
                      }
               };
           }
     }

This was tested with Spring Boot v2.7.2, Spring Cloud v2021.0.5

Leave a comment

Blog at WordPress.com.

Up ↑