How to send/set basic authorization i.e user and password to every request in openFeign client in spring boot with help of interceptor

huangapple go评论64阅读模式
英文:

How to send/set basic authorization i.e user and password to every request in openFeign client in spring boot with help of interceptor

问题

如何在Spring Boot的openFeign客户端中通过拦截器发送或设置基本授权,即用户和密码,以便在每个请求中发送
当我尝试在Spring Boot中为外部服务实现openFeign客户端时,该服务始终期望在请求标头中进行基本身份验证,即用户ID和密码,
我可以使用拦截器发送像下面这样的固定值,但这些值总是从属性中获取的:

@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);
    }
}

我希望能够从Feign API调用者那里接收这些授权信息,然后将其设置为服务的标头以访问资源,例如从Postman。

我该如何从请求标头中接收它并将其传递给Feign客户端以进行进一步处理?

How to send/set basic authorization i.e user and password to every request in openFeign client in spring boot with help of interceptor

英文:

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

答案1

得分: 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);
        }
    }
}
英文:

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);
        }
    }
}

答案2

得分: 0

你确定可以从每个请求中获取“授权”吗?

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

因为使用Feign进行服务调用时,默认情况下,每个Feign客户端将使用独立的Hystrix线程池。

英文:

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.

答案3

得分: 0

我最近解决了一个类似的问题,供参考:

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

import java.util.Map;

/**
 * 作者:chenwenshun@gmail.com 于 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();
        }
    }
}

你可以使用 setServiceContext 存放授权信息,使用 getServiceContext 获取授权信息,destroy 用于在请求完成后进行清理。

英文:

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&lt;Map&lt;String, String&gt;&gt; context = new HystrixRequestVariableDefault&lt;&gt;();

    public static Map&lt;String, String&gt; getServiceContext() {
        initServiceContext();
        return context.get();
    }

    public static void setServiceContext(Map&lt;String, String&gt; 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.

huangapple
  • 本文由 发表于 2023年3月8日 17:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定