【问题标题】:Camel HTTP authentication fails but call works with curlCamel HTTP 身份验证失败,但调用适用于 curl
【发布时间】:2013-09-09 17:21:42
【问题描述】:

我正在构建一个 Camel 路由,该路由将调用 Twilio's SMS API 以发送 SMS。 API 要求 BASIC authentication 但我的调用失败并出现 401 身份验证错误。我从 this worked example 复制了我的配置,并通过 curl 调用验证了我的凭据是正确的。但是,我无法弄清楚我在骆驼方面缺少什么。

我的 Camel HTTP 与不强制身份验证的 API 集成一切正常,并且 Twilio 调用的 URL 被正确提供(使用标头 *Exchange.HTTP_QUERY*)。

非常感谢任何帮助。谢谢。

这是我的 Blueprint-OSGi Camel 路线:-

<camelContext id="jellyfish-messaging-sms" errorHandlerRef="deadLetterQueue"
    trace="false" xmlns="http://camel.apache.org/schema/blueprint">

     <route id="sms.twilio">
        <from uri="activemq:sms.twilio" />

        <setHeader headerName="Content-Type">
            <constant>application/x-www-form-urlencoded</constant>
        </setHeader>
        <setHeader headerName="CamelHttpMethod">
              <constant>POST</constant>
        </setHeader>

         <to uri="https://api.twilio.com/2010-04-01/Accounts/{{sms.accountSid}}/SMS/Messages" />
    </route>

</camelContext>

<!-- BEANS -->

 <bean id="httpAuth" class="org.apache.camel.component.http.HttpConfiguration">
    <property name="authMethod" value="Basic"/>
    <property name="authUsername" value="${sms.accountSid}"/>
    <property name="authPassword" value="${sms.authToken}"/>
</bean>

<bean id="http" class="org.apache.camel.component.http.HttpComponent">
    <property name="camelContext" ref="jellyfish-messaging-sms"/>
    <property name="httpConfiguration" ref="httpAuth"/>
</bean>

这是一个例外(来自 karaf.log):-

2013-09-09 17:37:27,627 | INFO  | umer[sms.twilio] | HttpMethodDirector               | 366 - org.apache.servicemix.bundles.commons-httpclient - 3.1.0.7 | No credentials available for BASIC 'Twilio API'@api.twilio.com:443
2013-09-09 17:37:27,632 | ERROR | umer[sms.twilio] | jellyfish-messaging              | 266 - org.apache.camel.camel-core - 2.10.2 | Dead letter interceptor invoked
2013-09-09 17:37:27,635 | ERROR | umer[sms.twilio] | sms                              | 266 - org.apache.camel.camel-core - 2.10.2 | Exchange[ExchangePattern:InOnly, BodyType:String, Body:Nothing to see here, CaughtExceptionType:org.apache.camel.component.http.HttpOperationFailedException, CaughtExceptionMessage:HTTP operation failed invoking https://api.twilio.com/2010-04-01/Accounts/...snip.../SMS/Messages?From=%2B44...snip...&To=%2B44...snip...&Body=fred with statusCode: 401, StackTrace:org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking https://api.twilio.com/2010-04-01/Accounts/...snip.../SMS/Messages?From=%2B44...snip...&To=%2B44...snip...&Body=fred with statusCode: 401   at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:229) at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:157)

这是工作卷曲:-

curl -X POST https://api.twilio.com/2010-04-01/Accounts/...snip.../SMS/Messages -u ...snip...:...snip... -d "From=+44...snip..." -d "To=+44...snip..." -d 'Body=Hello'

【问题讨论】:

    标签: activemq apache-camel twilio http-authentication blueprint-osgi


    【解决方案1】:

    因此,在朋友 Eclipse 调试和 Charles 代理的帮助下,我找到了解决此问题的方法。出于某种原因,HttpConfiguration 和 HttpComponent 对象没有正确附加到 Camel 上下文,这意味着 Camel 正在生成另一个 HTTP 配置,它不知道身份验证详细信息。

    该解决方案绕过基于 bean 的身份验证解决方案,而是使用端点上的身份验证选项。它还将消息正文设置为查询参数(我根据消息正文的内容在 bean 中构建),而不是使用 *Exchange.HTTP_QUERY* JMS 标头(又名 CamelHttpQuery)。

    <route id="sms.twilio">
      <from uri="activemq:sms.twilio" />
        <unmarshal ref="smsProto" />
    
        <!-- set the query params to be sent -->
        <transform>
          <method bean="Sender" method="getQuery"/>
        </transform>
    
        <to uri="activemq:sms.twilio?preserveMessageQos=true" />
    </route>
    
    <route id="sms.twilio.send">
      <from uri="activemq:sms.twilio.send" />
    
      <setHeader headerName="Content-Type">
        <constant>application/x-www-form-urlencoded</constant>
      </setHeader>
      <setHeader headerName="CamelHttpMethod">
        <constant>POST</constant>
      </setHeader>
    
     <to uri="https://api.twilio.com/2010-04-01/Accounts/{{sms.accountSid}}/Messages?authMethod=Basic&amp;authUsername={{sms.accountSid}}&amp;authPassword={{sms.authToken}}" />
    </route>
    

    我很想知道为什么基于 bean 的身份验证不起作用。

    J.

    【讨论】:

      【解决方案2】:

      我遇到了类似的情况,我的 .to(http://) 端点((具有与您类似的 Bean 配置)会突然抛出 HTTP 403 Forbidden 响应,而它曾经正常工作并且在使用时仍然正常工作卷曲。

      这里的诀窍是使用抢先式身份验证。即使您在 Camel 端点(通过 bean 或端点本身)上正确配置了 authUsername、authPassword 和 authMethod,Camel 也不会在第一个请求中发送身份验证详细信息。在发送新请求中的身份验证详细信息之前,它需要 401 Unauthorized。

      我只是通过使用 tcpdump 检查请求才发现这一点,但解决方案很简单:

      .to("http://endpoint" + "?httpClient.authenticationPreemptive=true")
      

      来源: http://camel.465427.n5.nabble.com/HTTP-Basic-Authentication-tp5742229p5742347.html 文档: http://camel.apache.org/http.html

      【讨论】:

        猜你喜欢
        • 2017-10-29
        • 1970-01-01
        • 1970-01-01
        • 2020-01-30
        • 2021-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-07
        相关资源
        最近更新 更多