I have two Feign clients in Spring Boot doing different things, but would like them to be authenticated differently.
@FeignClient(
name = "...",
url = "${url1}",
configuration = Config1.class
)
public interface Client1 {
@PostMapping(
path = "...",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
JsonNode doThing(@RequestBody JsonNode thing);
}
@FeignClient(
name = "...",
url = "${url2}",
configuration = Config2.class
)
public interface Client2 {
@PostMapping(
path = "...",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
JsonNode doThing(@RequestBody JsonNode thing);
}
They both need basic authentication, but different values for username and password. For that, I thought about having separate Config
classes to set their respective clients:
@Configuration
public class Client1 {
private final String user;
private final String password;
public Client1(final Config1 config) {
this.user = config.getUser();
this.password = config.getPassword();
}
@Bean(name = "client1")
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor(user, password);
}
}
@Configuration
public class Client2 {
private final String user;
private final String password;
public Client1(final Config2 config) {
this.user = config.getUser();
this.password = config.getPassword();
}
@Bean(name = "client2")
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor(user, password);
}
}
But my API is returning HTTP 4xx errors, as if the interceptor did not work at all. Can I get some pointers on setting this up properly?
(Notice that I gave those beans name
s, because they will otherwise conflict for DI.)
I guess You have to remove the stereotype @Configuration
.
I actually came here during a search to a similar problem. I do have (same like You) two different configs. One FeignClient with and a second client without auth. But the second client is using both RequestInterceptors (I implemented a noop-RequestInterceptor just for logging).
Could You actually solve your problem?