【问题标题】:Proxy authentication with Camel using ProducerTemplate使用 ProducerTemplate 使用 Camel 进行代理身份验证
【发布时间】:2013-06-03 19:15:49
【问题描述】:

我有一个使用 Camel 并在 ServiceMix 服务器上运行的项目,但我似乎无法让它访问外部 Web 服务,我怀疑这是因为我无法正确设置代理身份验证。

Exchange exchange = producerTemplate.request(url, new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
        exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/json");
    }
});
response = exchange.getOut().getBody(String.class);

如果我在最后一行设置断点,我会在交换对象中看到 ConnectionTimedOutException 并且响应为空。

我尝试了多种方式设置代理。

1) 我尝试在实现CamelContextAware的类中设置代理设置:

camelContext.getProperties().put("http.proxyHost", "...");
camelContext.getProperties().put("http.proxyPort", "8080");
camelContext.getProperties().put("http.proxyUser", "...");
camelContext.getProperties().put("http.proxyPassword", "...");
camelContext.getProperties().put("http.proxySet", "true");

这在独立模式下工作,但是当我在 ServiceMix 中部署代码时,camelContext 对象为空。

2)我尝试在ServiceMix的etc/system.properties文件中设置代理设置。

3) 我尝试在 camel-context.xml 中使用 http-conf:conduit,如下所示:

<http-conf:conduit name="*.http-conduit">
    <http-conf:client ProxyServer="..." ProxyServerPort="8080" />
    <http-conf:proxyAuthorization>
        <conf-sec:UserName>...</conf-sec:UserName>
        <conf-sec:Password>...</conf-sec:Password>
    </http-conf:proxyAuthorization>
</http-conf:conduit>

但是,我认为这只有在我使用 cxf 客户端时才有效。

没有任何效果,我需要它在部署到 ServiceMix 时工作。 任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: apache-camel apache-servicemix


    【解决方案1】:

    如果我是对的,问题更像是您无法访问 SMX 中的属性。 ServiceMix 支持 SpringDM,我建议使用它来向 Configuration Admin 请求属性。

    0) 假设您使用的是 Spring,您的 main-appContext xml 必须放在 resources/META-INF/spring/ 下,SMX 将在那里寻找它来初始化您的应用程序。

    1) 向 ServiceMix/etc 添加不同的属性文件(不要使用 system.properties)。它必须命名为 *.cfg,即:my.properties.cfg

    1.5) 确保配置已加载。在 SMX 中,输入:

    config:update
    config:list | grep my.prop.name
    

    2) 您需要 Spring DM 解析此文件。添加 appcontext xml 内容,如下所示(确保您拥有命名空间)。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"
        xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/osgi-compendium 
           http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">
    
        <osgix:cm-properties id="myProperties" persistent-id="my.properties" /> <!--without the extension-->
        <context:property-placeholder ignore-unresolvable="true" properties-ref="myProperties" />
    </beans>
    

    3.) 也导入此 appcontext 并通过 ${my.prop} 占位符使用属性。

    希望这会有所帮助!

    【讨论】:

    • 补充一点:如果你想在 Camel 路由中使用属性,你将不能像占位符那样使用 ${foo}。您必须使用 Properties 组件并在路由中使用 {{foo}} 之类的占位符。 见:camel.apache.org/properties.html
    【解决方案2】:

    试试这个代码:

    HTTPConduit conduit = (HTTPConduit)outMessage.getExchange().getConduit(outMessage);
    HTTPClientPolicy policy = conduit.getClient();
    policy.setProxyServer(PROXY_IP);
    policy.setProxyServerPort(PROXY_PORT);
    conduit.setClient(policy);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 2017-11-24
      • 2019-01-31
      • 2012-03-18
      • 2015-09-15
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多