Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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())
        }
    }
}
like image 698
Mohammad Reza Kianifar Avatar asked Mar 10 '26 13:03

Mohammad Reza Kianifar


1 Answers

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.

like image 165
Mohammad Reza Kianifar Avatar answered Mar 13 '26 09:03

Mohammad Reza Kianifar