0

How to send or set basic authorization, i.e., user and password, to every request in the openFeign client in Spring Boot with help for the interceptor As I am trying to implement an openFeign client for an external service in spring boot, which always expects basic authentication in its request header, i.e., user ID and password, I can send fixed values like the ones below with interceptor, but it is always fix values that picked from properties:

@Configuration
@EnableFeignClients(basePackages = {"com.abc.xyz.feign"})
public class CsdClientConfig {
    @Value("${cds-service-provider.userId}")
    private String userId;
    @Value("${cds-service-provider.password}")
    private String password;

    @Bean
    BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor(userId,password);
    }

I want to receive this auth info from a feign api caller for every api call and set it to header of service to access resources .i.e from postman

How can I accept it from the request header and pass it to feign client for further processing?

[! [enter image description here] 1]1

3 Answers3

0

I found the way as below: So we can use RequestInterceptor to inject headers from every request to the feign client on every request.

@Component
@AllArgsConstructor
public class FeignClientInterceptor implements RequestInterceptor {
    final HttpServletRequest request;
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String reqAuthInput= request.getHeader("authorization");
        if (reqAuthInput!= null) {
            requestTemplate.header("authorization",reqAuthInput);
        }
    }
}
Toni
  • 3,296
  • 2
  • 13
  • 34
0

Are you sure this can get 'authorization' from per request?

String reqAuthInput= request.getHeader("authorization");

bacause using Feign for service calls, by default, each Feign client will use an independent Hystrix thread pool.

chenws
  • 11
  • 2
  • 1
    Basically, in my case, I observed that on every request enter into my spring project working as a consumer for an external service pass through interceptor here hence irrespective of what you mentioned above it come here and check for request parameters. – Ruturaj Shinde May 19 '23 at 08:59
0

I just recently solved a similar problem, for reference:

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;

import java.util.Map;

/**
 * @author chenwenshun@gmail.com on 2023/5/16
 */
public class ServiceContextHolder {

    private static final HystrixRequestVariableDefault<Map<String, String>> context = new HystrixRequestVariableDefault<>();

    public static Map<String, String> getServiceContext() {
        initServiceContext();
        return context.get();
    }

    public static void setServiceContext(Map<String, String> contexts) {
        initServiceContext();
        context.set(contexts);
    }

    private static void initServiceContext() {
        if (!HystrixRequestContext.isCurrentThreadInitialized()) {
            HystrixRequestContext.initializeContext();
        }
    }

    public static void destroy() {
        if (HystrixRequestContext.isCurrentThreadInitialized()) {
            HystrixRequestContext.getContextForCurrentThread().shutdown();
        }
    }
}

you can use setServiceContext put authorization and getServiceContext get authorization, destroy for after request complete.

chenws
  • 11
  • 2