Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to consume basic-authentication protected Restful web service via feign client

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.

like image 765
Zhuo YING Avatar asked Sep 21 '16 02:09

Zhuo YING


People also ask

How do you pass authorization in feign client?

Since in Feign Client you usually do everything in the interface, the same will be for the Authorization header. What you need to do is to simply pass your Authorization header in the declaration of your Feign Client method, as an @RequestHeader argument.

How does feign client call REST API?

Let's implement the Feign in our project and invoke other microservices using Feign. Step 1: Select currency-conversion-service project. Step 2: Open the pom. xml and add the Feign dependency.

Can we use feign client without Eureka?

6.6 Example: How to Use Ribbon Without Eureka Eureka is a convenient way to abstract the discovery of remote servers so that you do not have to hard code their URLs in clients. However, if you prefer not to use Eureka, Ribbon and Feign also work.


2 Answers

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)
like image 67
Juan Pablo G Avatar answered Nov 17 '22 09:11

Juan Pablo G


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.

like image 26
Robert Robertino Avatar answered Nov 17 '22 10:11

Robert Robertino