I would like to use a @FeignClient in a simple spring boot application (CommandLineRunner) to call a microservice endpoint. How can I provide an OAuth2Authentication to call a protected endpoint like helloUser()
?
@FeignClient(name = "sampleService", contextId = "greetingService")
public interface GreetingService {
@GetMapping("/hello-anonymous")
String helloAnonymous();
@GetMapping("/hello-user")
@Secured({ Role.USER })
String helloUser();
@GetMapping("/hello-admin")
@Secured({ Role.ADMIN })
String helloAdmin();
}
You can use Feign RequestInterceptor to pass the auth header downstream:
public class FeignRequestInterceptor implements RequestInterceptor {
@Override
public final void apply(RequestTemplate template) {
template.header("Authorization", "foo token");
}
}
This way all the feign calls will be provisioned with an auth header.