How to use basic authentication. Secure web service via feign client

Thank you for your time. To make it simple, I created an example service, as shown below:

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

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

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

When I start this service and access it like this:

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

Everything goes well.

In another node, I created a “service compiler” using netflix feign. This is the 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 configure the header of the "Authorization" request.

Any idea? Thanks again.

+5
source share
1 answer

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 the @FeignClient annotation use this configuration file

 @FeignClient(name="service",configuration = FeignClientConfiguration.class) 

go and try, hope this helps

Thanks to Ryan Baxter for fixing it.

+9
source

All Articles