【问题标题】:How to set outgoing Host header from Camel http4 proxy如何从 Camel http4 代理设置传出主机标头
【发布时间】:2016-09-10 03:39:12
【问题描述】:

我正在使用 Apache Camel 2.13.1 作为 HTTP 代理,使用 http4 组件:

.from("servlet://*?matchOnUriPrefix=true")
.to("http4://example.com/?bridgeEndpoint=true&httpClient.redirectsEnabled=false")

现在我需要将传出代理请求的Host HTTP 标头参数设置到后端系统,因为它被代理应用程序用来生成绝对链接,它必须匹配公共/前端 url。 使用简单的方法在骆驼消息.setHeader("Host", "foo.com") 中简单地设置Host 标头失败,因为http4 组件用代理主机example.com 的主机名覆盖了它。

进一步的研究表明,HTTPClient 曾经通过虚拟主机参数来执行此操作。 Camel 的 http4 组件支持使用 httpClient 参数传递 HTTPClient 参数。但是从 2.13.0 版本开始, camel http4 使用 HTTPClient 4.3 的 builder api (http://camel.apache.org/http4.html) 从端点配置中传递 httpClient.* 参数,不幸的是 HTTPClient 4.3 的 builder api 不再包含虚拟主机参数。从这个 HTTPClient 邮件列表回复 (https://mail-archives.apache.org/mod_mbox//hc-httpclient-users/201312.mbox/%3C1387792931.6163.17.camel@ubuntu%3E) 来看,我可能必须通过 setTargetHost 在 HttpClientContext 上设置虚拟主机,它似乎被称为目标主机。我怎样才能通过骆驼在两者之间做到这一点?

所以总结一下:我使用的是camel的http4组件,需要更改出局代理请求的HostHTTP头值。

【问题讨论】:

    标签: apache-camel apache-httpclient-4.x


    【解决方案1】:

    您可以像这样使用 httpContext 选项设置 HttpContext 实例。请确保 HttpContext 实例与注册表中的“customerContext”绑定。

    http4://localhost:8081?httpBindingRef=customBinding&httpClientConfigurerRef=customConfigurer&httpContext=#customContext
    

    【讨论】:

    • 我接受这个作为答案,因为它记录了如何从 Camel 获得对 HttpClient 内部的访问权限,这最终会导致成功。但是我自己无法让它工作,因为 HttpClient 配置非常复杂,我用完了时间。
    • HttpClient 配置对您没有帮助,您需要根据需要应用 customContext 来设置 Host 值。
    • 这为我解决了这个问题:stackoverflow.com/questions/9499697/…
    【解决方案2】:

    关注这个帖子Configuring Apache HttpClient to access service through proxy/load-balancer (overriding Host header)

    这个 sn-p 对我有用:

                HttpComponent http4 = camelContext.getComponent("http4", HttpComponent.class);
    
                http4.setHttpClientConfigurer(new HttpClientConfigurer() {
    
                    @Override
                    public void configureHttpClient(HttpClientBuilder builder) {
    
                        builder.addInterceptorFirst(new HttpRequestInterceptor() {
                            @Override
                            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                                request.setHeader(HTTP.TARGET_HOST, publicUrl);
                            }
                        });
                    }
    
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多