Support different custom configurations for multiple feign clients with the same name
See original GitHub issueusing @FeignClient
in spring currently has one fatal issue, which is
demonstrated with this repository. If I got two feign clients, each having it’s own configuration (excluded from component scan), and one of them has an RequestInterceptor
, both (and all others, if present) get this interceptor also applied. The repository shows the following experiment to show this:
experiment
We assume some foreign service, serving “foo” entities. You have to be authenticated to access this the foo resource and have a role “USER”. If you have “ADMIN”, you will see different data, as when “USER”.
This behavior is implemented using plain spring boot with spring security in the “producer” service. Additionally it registers to an eureka instance, so some “consumer” service is able to fetch this.
So the consumer service should fetch data from consumer, automatically passing basic auth of “users he know”. While this may not clearly makes sense for real world, something very similar is happening consuming OAuth2 secured resource servers with client credentials grant! So to get the data correctly, we define 2 clients, with individually set up basic auth credentials using feign client configuration.
Since the main application
does not use @SpringBootApplication
, but @EnableAutoConfiguration
,
and an exclude filter on @ComponentScan
, the two different configuration
do not declare the internal beans.
So the expected result of these 2 clients is: the UserFooClient
retrieving
at least a foo entity with value “user1”, while the AdminFooClient
s result
should contain a “admin1” foo.
To verify, which result is intended, the same test is done using plain old
RestTemplate
.
consequences
This issue leads towards we can’t really use multiple feign clients in spring boot
using the @FeignClient
annotation, because as soon one client is using
some authorization flow (basic, oauth2), this is applied to all other
clients.
Issue Analytics
- State:
- Created 7 years ago
- Comments:37 (26 by maintainers)
@xetys Based on @spencergibb’s suggestion you can do something like this to accomplish what you want
You could probably also integrate Ribbon into the situation as well.
You don’t need all that infrastructure to reproduce this problem. All you need is 2 feign clients with the same name and different configurations (they can’t actually have different configuration because the configuration is keyed on the name).