密码的骆驼组件端点选项被更改,如何防止这种情况? [英] Camel component endpoints options for password is altered, how to prevent this?

查看:22
本文介绍了密码的骆驼组件端点选项被更改,如何防止这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Camel 中,我使用 http4 组件在远程服务器上发出 REST 请求.

In Camel I am using the http4 component to make REST request on a remote server.

组件 documentation 指出凭据应该像这样放在端点上的选项中:

The component documentation states that the credentials should be put in the options on the endpoint like this:

https4://myremote.server.com/?authUsername=xxx&authPassword=yyy

这一直运行良好,直到有人在另一个环境中输入带有+"字符的密码.我们注意到+"字符在服务器中作为空格传输,这会产生错误.通过在 Camel 文档中进行更深入的搜索,我们发现 a page 在那里进行了解释是一个RAW"函数,可以这样使用:

This was working well until someone put a password with a '+' character on another environment. We notice that the '+' character is transmitted as a space in the server which generates an error. By searching deeper in the Camel documentation we found a page explaining there is a "RAW" function to use like this:

https4://myremote.server.com/?authUsername=xxx&authPassword=RAW(yyy)

保持密码不变.

很遗憾,此功能仅在 Camel 2.11 版本中引入,目前我们不打算升级到 ServiceMix 5.1.x.

Unfortunately this function has only been introduced in the Camel 2.11 version and for the moment we are not planning to upgrade to ServiceMix 5.1.x.

我们目前使用的是 serviceMix 4.5.x,camel 版本是 2.10.7.

We are currently on serviceMix 4.5.x and the camel version is 2.10.7.

我在路线中尝试了这些(一一):

I tried these in the route (one by one):

  • .setProperty("Authorization", "基本{base64Hash}")
  • .setHeader("Authorization", "Basic {base64Hash}")
  • .setProperty(HttpHeaders.AUTHORIZATION, "基本 {base64Hash}")
  • .setHeader(HttpHeaders.AUTHORIZATION, "基本 {base64Hash}")

但远程服务器向我发送了 401(未经授权).

but the remote server sends me a 401 (Unauthorized).

问题是:除了使用端点上的选项之外,还有其他方法可以为 http4 组件发送凭据吗?

The question is: is there any other alternative to send credentials for http4 component than using the option on the endpoint?

推荐答案

我终于找到了一种方法,我遇到的问题是服务器需要一个名为:X-Forwarded-Proto 的额外参数,那么下面的方法效果很好,而不是在端点选项中传递凭据:

I finally found a way and the problem I had was the server that need an extra parameter called : X-Forwarded-Proto, then the following way works very well instead passing the credentials in the endpoint option:

from(in.getEndpointUri())
    .setHeader(Exchange.HTTP_METHOD, constant("GET"))
    .setHeader(Exchange.HTTP_PATH, simple("/path/to/my/resource/1234"))
    .setHeader(Exchange.HTTP_QUERY, constant("type=accessories&view=blue"))
    .setHeader("X-Forwarded-Proto", constant("https"))
    .setHeader("Authorization", constant("Basic bXl1c2VybmFtZTpwYXNzd29yZDEyMzQ="))

    .to("https4://myremote.server.com/myrestservices")
    .convertBodyTo(String.class)
    ;

之后,使用 bean 或处理器生成 Base64 哈希将很容易.

After, using a bean or processor to generate the Base64 hash would be easy.

这篇关于密码的骆驼组件端点选项被更改,如何防止这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆