【问题标题】:How to set basic authentication with apache camel and http4如何使用 apache camel 和 http4 设置基本身份验证
【发布时间】:2021-07-23 22:38:31
【问题描述】:

我正在尝试使用 Apache Camel 的 Http4 组件连接到需要基本身份验证的 HTTP URL。我正在通过 Exchange.HTTP_QUERY 标头从骆驼处理器设置凭据。我这样配置我的路线:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint ...>

<bean id="myProcessor"
    class="com.myCompany.Bean"
    factory-method="myProcessorBean">
</bean>

<camelContext id="myContext" xmlns="http://camel.apache.org/schema/blueprint">
    
    <route id="myRoute>
        <from uri="activemq:queue:myQueue" />
        <process ref="myProcessor" />
        <to uri="http4://oldhost"/>
    </route>
        
</camelContext>

</blueprint>

我的处理器看起来像:

public void process(Exchange exchange) throws Exception {
  String user = getUserFromDB();
  String pwd = getPasswordFromDB();

  String queryParameters = "authMethod=Basic&authPassword="+pwd+"authUsername="+user;
  exchange.getIn().setHeader(Exchange.HTTP_QUERY, queryParameters);
  exchange.getIn().setHeader(Exchange.HTTP_URI, "api.abc_company.com/service/to/consume");
  ....
}

结果:

Error myContext: org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking https://api.abc_company.com/service/to/consume?authMethod=Basic&authPassword=xxxxxx&authUsername=test with statusCode: 401

似乎密码已更改为 xxxxxx,但我不确定。

当我尝试在 URI 中硬编码用户和密码时效果很好,但是我需要一种方法来以编程方式设置这些值,因为用户和密码存储在数据库中。

【问题讨论】:

    标签: apache-camel apache-karaf


    【解决方案1】:

    "authMethod=Basic&authPassword="+pwd+"authUsername="+user;

    您需要在每个 uri 参数之间加上 & 。您在密码后缺少一个。

    试试:

    String queryParameters = "authMethod=Basic&authPassword="+pwd+"&authUsername="+user;
    

    您还应该在生产者端点 URI http4://oldhost?authMethod=Basic&amp;authPassword="+pwd+"&amp;authUsername="+user 而不是标头中定义凭据:Exchange.HTTP_QUERY

    如果您想在处理器中执行此操作,您可以尝试手动设置 Authorization headerbasic &lt;credentials&gt; 其中 &lt;credentials&gt; 是 base64 编码的 username:password

    调用 https://api.abc_company.com/service/to/consume 失败?

    如果您想连接到 https 站点,请使用 https4://

    【讨论】:

    • 其实我已经在使用&了,这是示例中的一个错误。我测试了设置标题,它工作正常。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2020-06-02
    • 2012-02-04
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多