pesche666
pesche666

Reputation: 179

Using @FeignClient with OAuth2Authentication in Javaclient

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();
}

Upvotes: 0

Views: 371

Answers (1)

Anton Hlinisty
Anton Hlinisty

Reputation: 1467

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.

Upvotes: 1

Related Questions