0

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();
}
pesche666
  • 139
  • 1
  • 13

1 Answers1

1

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.

Anton Hlinisty
  • 1,441
  • 1
  • 20
  • 35