【问题标题】:How to pass parameters to FeignRequestInterceptor?如何将参数传递给 Feign 请求拦截器?
【发布时间】:2020-03-19 19:29:50
【问题描述】:

我有这个代码

@Component()
public class FeignRequestInterceptor {

    @Bean
    public RequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("username", "password");
    }
}

有没有办法可以将用户名和密码作为参数传递?

我有一个拦截请求的过滤器,在那里我得到了一些标头,我想使用这些标头来设置 userpassword 所以当我稍后使用 feign 客户端处理另一个请求时,我'有那些标题

【问题讨论】:

  • 您能否提供您的过滤器的源代码以便获得更具体的答案?

标签: java spring-boot spring-cloud-feign feign


【解决方案1】:

伙计们,我找到了这个解决方案,不知道它是否是最好的,但它确实有效。

我从上面删除了代码,甚至删除了过滤器来拦截请求,我使用了这个

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = "Basic " + Base64.getEncoder().encodeToString(
                (request.getParameter("username") + ":" + request.getParameter("password")).getBytes(Charsets.UTF_8));
        template.header("Authorization", token);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2017-03-08
    • 2011-10-07
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2018-02-02
    相关资源
    最近更新 更多