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.
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.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With