Search code examples
spring-bootkotlinfeign

How to set a custom Feign RequestInterceptor for specific clients?


I need to add custom Authorization header to some new feign clients. So I write an RequestInterceptor and it worked but the point is I don't want this custom RequestInterceptor affect my old clients. I tried to filter using template.url() method but it doesn't give me the entire url of the request and it only contains the client method url (not url and path which is announced above the client class). My Question is that how can I target the interceptor?

This is my configuration:

@Configuration
open class FeignCustomConfiguration {

    private fun generateToken(): String { ... }

    @Bean
    open fun requestInterceptor(): RequestInterceptor {
        return RequestInterceptor {
            it.header("Authorization", generateToken())
        }
    }
}

Solution

  • I found the solution. For each FeignClient there is a configuration option which accepts an array of classes. The syntax of assigning a class to configuration in kotlin is as follow:

    @FeignClient(
            name = "feign-client",
            path = "/path",
            url = "https://example.com",
            configuration = [FeignCustomConfiguration::class]
    )
    interface FeignCustomClient {
         ...
    }
    

    With this assignment, each FeignClient has its own configuration and RequestInterceptor doesn't deal with other clients.