24

Thank you for your time. To make it simple, I created a example service like below:

@RestController
@RequestMapping("/")
public class ComputeController {

    @GetMapping("/add")
    public int add(@RequestParam("left") int left, @RequestParam("right") int right) {
        return left + right;
    }
}

To protected this url, I config spring-security like this:

management.security.enabled=true
security.user.name=admin
security.user.password=admin

When I startup this service and access like this:

GET /add?left=100&right=11 HTTP/1.1
Authorization: ***** Hidden credentials *****
Host: localhost:7777
Connection: close

Everythis is going fine.

In other node, I created a "service-comsumer" by netflix feign. It's a Java Interface.

@FeignClient(name = "API-GATEWAY", path = "/compute-service", fallback = ComputeServiceCircuitBreaker.class)
public interface ComputeServiceClient {

    @RequestMapping(path = "/add", method = RequestMethod.GET)
    public Integer add(@RequestParam("left") Integer left, @RequestParam("right") Integer right);
}

But I DO NOT know how to config the request header "Authorization".

Any idea? Thanks again.

Zhuo YING
  • 972
  • 3
  • 11
  • 19
  • 2
    I found the answer. http://stackoverflow.com/questions/35491581/basic-authentication-service-called-by-zuul?rq=1 – Zhuo YING Sep 21 '16 at 03:32

2 Answers2

55

You need to create a FeignClient Configuration class, for example

import feign.auth.BasicAuthRequestInterceptor;

@Configuration
public class FeignClientConfiguration {
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
         return new BasicAuthRequestInterceptor("admin", "admin");
    }
}

then in your @FeignClient annotation use this configuration file:

@FeignClient(name="service", configuration = FeignClientConfiguration.class)
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Juan Pablo G
  • 879
  • 8
  • 8
  • 9
    Using `@Configuration` here causes this bean to be applied to *all* `@FeignClient` and you do not need the `configuration =` part. If you remove `@Configuration` then this bean is ignored, and the `configuration =` part does nothing. – Ariel Aug 31 '18 at 19:18
  • 2
    The correct way to do this (if you want it just on one specific `@FeignClient`) is to add `@Configuration` and then exclude it from `@ComponentScan` with `excludeFilters` – Ariel Aug 31 '18 at 22:12
  • why would it be applied to all if `configuration` attribute is not used with `@FeignClient`. – Ravik Feb 25 '19 at 13:36
  • 2
    @Ravik because it is picked up by component scan. For those confused by this (as I was) read [documentation](https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#spring-cloud-feign-overriding-defaults) – Jan Mares Jun 04 '19 at 11:49
  • 3
    Thanks, this works for me. One comment, be careful, use `feign.auth.BasicAuthRequestInterceptor`, not `org.springframework.http.client.support.BasicAuthenticationInterceptor`... – John Smith Nov 20 '19 at 02:14
13

As of october 2020, this works:

public class FeignClientConfiguration {

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("asdf", "asdf");
    }
}


@FeignClient(name = "thirdPartyClient", url = "ceva.com",
        configuration = FeignClientConfiguration.class)
public interface ThirdPartyClient {

    @GetMapping
    Response get();
}

Note, we don't annotate the configuration with @Configuration in order to not apply it to all requests.

  • this did not work for me when `FeignClientConfiguration` has in itself `@Value` properties – Mugen Jul 18 '22 at 12:15
  • it works just fine even with Value properties. Anything you're doing to not make it work, is wrong. `public Config(@Value("${asdf}") String asdf, @Value("${asdf") String asdf){}` ` – Robert Robertino Jul 25 '22 at 15:30