4

I have a service that gets http request with an authorization header. When processing the request, I want to use a Feign Client to query another service. The query to the other service should include the different authorization header.

3 Answers3

7

You simple pass your header as an argument and you declare your method in your interface accordingly. Please find snippet of code below.

And when you run your Feign client with logging level full (feign.client.config.default.loggerLevel=full), you'll see the authorization header (being an OAuth Bearer access token in my case).

Hopefully this helps.

All the best, Wim

@FeignClient(name="mde", url="${MDE.campaignServiceEndpoint}")
public interface CampaignClientService {
    @RequestMapping(method = RequestMethod.GET, value = "/useCases/search/findByName?name={name}")
    @Cacheable("placementUseCase")
    PlacementUseCase findUseCaseByName(@RequestHeader(value = "Authorization", required = true) String authorizationHeader, @PathVariable("name") String name);
1

If you are using Feign client directly without spring

then you can pass Header params something like this, in Feign Interface

@Headers("Content-Type: application/json")
@RequestLine("PATCH infusionsoft/contacts/{contactId}?format={format}")
InfusionsoftFullContact updateContact(@Param("contactId") Long contactId,
                                      @Param("format") String format,
                                      InfusionsoftFullContact contact,
                                      @HeaderMap Map<String, Object> headerMap);

Where HeaderParams can be as below

Map<String, Object> headerMap = new HashMap<>();
    
headerMap.put("application", "FDX");
headerMap.put("Authorization", "Basic aW5mdXNpb25zb2Z0OnBhc3N3b3Jk");
headerMap.put("platform",user.getPlatform());
headerMap.put("platformKey", user.getPlatformKey());
headerMap.put("email",user.getEmail());
headerMap.put("Content-Type", MediaType.APPLICATION_JSON);
Nexonus
  • 706
  • 6
  • 26
Umashankar
  • 71
  • 5
0

Create Header like this and pass to your feign client

private HttpHeaders getHeaders(final HttpServletRequest httpServletRequest) {
        final HttpHeaders headers = new HttpHeaders();
        headers.add("authorization", httpServletRequest.getHeader("authorization"));
        return headers;

Example 1

Or very simple add intercepter

@Component
public class AuthFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
            template.header(HttpHeaders.AUTHORIZATION, httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
        }
    }
}

Example 2