We have multiple feign clients in one of our services which should use different request interceptors. For example we have clients that are requesting data from other services based on an API endpoint and use the users oauth token along the way. For this we use the provided OAuth2FeignRequestInterceptor.
On the other hand, the service does some work based on RabbitMQ messages and uses a fixed token. Because of this, we can't use the OAuth2FeignRequestInterceptor, instead we provide our own interceptor. Currently, both Interceptors are loaded in all cases which doesn't work for us.
Is it possible to provide a way to configure request interceptors on a client basis, e.g through @FeignClient(requestInterceptors={}) or FeignConfiguration?
The only solution to this problem is to create FeignClients based on the provided Builder. But maybe there's a better solution to it
Comment From: ryanjbaxter
You should be able to create a bean in a specific configuration class for each feign client
Comment From: lukas-stadler
If I have for example a configuration like this:
@Configuration
@EnableOAuth2Client
public class OAuthTokenFeignConfig {
/**
* Enables forwarding of a token by feign clients.
*/
@Bean
public OAuth2FeignRequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) {
return new OAuth2FeignRequestInterceptor(oauth2ClientContext, new ImplicitResourceDetails());
}
}
Then I have a feignClient that uses this cofiguration
@FeignClient(name = "test", url = "${test.host}", configuration = OAuthTokenFeignConfig.class)
public interface OAuthClient {
//some defined endpoints here...
}
Further, I have another configuration
@Configuration
@EnableOAuth2Client
public class FixedTokenFeignConfig {
}
and another feignClient that uses this FixedTokenFeignConfig
@FeignClient(name = "test", url = "${test.host}", configuration = FixedTokenFeignConfig.class)
public interface FixedTokenTestClient extends {
//some defined endpoints here...
}
The problem now is, that the FixedTokenTestClient also picks up the OAuth2FeignRequestInterceptor (because it is automatically found) although I want to use a different RequestInterceptor that provides a fixed token.
Comment From: spencergibb
Either remove @Configuration from OAuthTokenFeignConfig or exclude it from scanning.
Comment From: lukas-stadler
Unfortunately removing @Configuration doesn't help. It is still picked up. Removing @Bean from
public OAuth2FeignRequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) {
return new OAuth2FeignRequestInterceptor(oauth2ClientContext, new ImplicitResourceDetails());
}
makes it not recognize the RequestInterceptor anymore and therefore is not loaded. I also removed all the other Annotations that have @Configuration on it, doesn't make a difference.
Edit: I also moved the class outside of scanBasePackageClasses, no difference
Comment From: spencergibb
I see my error, the interceptor bean needs to go in FixedTokenFeignConfig and that's the one to remove the annotation from.
Comment From: lukas-stadler
I think it is correct. The FixedTokenFeignConfig eventually will load its own Interceptor. It shouldn't have the OAuth2FeignRequestInterceptor autowired. So your initial comment was correct but unfortunately it doesn't help. I have also tried with no @Configuration on both *FeignConfig classes. Makes no difference (I have them wired through @FeignClient
Comment From: spencergibb
if OAuth2FeignRequestInterceptor is a bean in the parent context, all feign clients will get it. If it is only a bean in a specific feing client configuration, only that client will get it.
Comment From: lukas-stadler
So like the example above, the OAuth2FeignRequestInterceptor is a Bean inside the OAuthTokenFeignConfig Configuration and therefore it should only be part of that client that has the OAuthTokenFeignConfig configured.
That's exactly what I have. But still it get's wired into the other FeignClient as well. Let me know if I misunderstand and I need to create somehow a specific scope only for that specific client (but I would think that this is a bit overkill for what I try to achieve here.
Comment From: spencergibb
because OAuthTokenFeignConfig has @Configuration it's being picked up and put in the parent context, therefore it is available to all feign clients.
At this point, I need a complete, minimal, verifiable sample sample that reproduces the problem. It should be available as a GitHub (or similar) project or attached to this issue as a zip file.
Comment From: lukas-stadler
I have found the issue. I had the same name set for both FeignClients and because of that, it reused the RequestInterceptors. Without @Configuration and different names, it works.
Comment From: dmudired
I faced the same problem, with multiple feign clients and the solution was removing @Configuration annotation from one of the CustomFeignConfig file.
Comment From: arthur-mts
yes, removing @Configuration annotation from the config file worked for me too