spring-cloud-netflix icon indicating copy to clipboard operation
spring-cloud-netflix copied to clipboard

It is not possible to specify different RequestInterceptors for different clients

Open DmtryJS opened this issue 5 years ago • 1 comments

Describe the bug There are two different clients. It is necessary for each service to send its own headers, which are added via the Interceptor. Only one bin works with two clients at the same time.

Sample

@FeignClient(
        name = "service_one",
        path = "service_one_path",
        configuration = FeignConfigOne.class,
)
public interface ClientOne {

    @GetMapping("/one")
    Object get();
}
@FeignClient(
        name = "service_two",
        path = "service_two_path",
        configuration = FeignConfigTwo.class,
)
public interface ClientTwo {

    @GetMapping("/two")
    Object get();
}
@RequiredArgsConstructor
public class FeignConfigOne {

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor() {
       return template -> {
            String jwtToken = "tokenOne";
            template.header(HttpHeaders.AUTHORIZATION, "Bearer " + jwtToken);
        };
    }
@RequiredArgsConstructor
public class FeignConfigTwo {

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor() {
       return template -> {
            String jwtToken = "tokenTwo";
            template.header(HttpHeaders.AUTHORIZATION, "Bearer " + jwtToken);
        };
    }

Somewhere in the service call

private ClientOne clientOne;
private ClientTwo clientTwo;

void doCall() {
clientOne.get();
clientTwo.get();
}

Enabled logging shows that the call passes with the same token for both clients

DmtryJS avatar Sep 15 '21 17:09 DmtryJS

My mistake, there was a transitive dependency where such a bean was already declared

DmtryJS avatar Sep 15 '21 19:09 DmtryJS