Apache Camel 将内部 headers 发送到 HTTP 调用

Apache Camel sends internal headers to HTTP calls

默认情况下,Apache Camel 将消息中存在的所有 headers 转换为 HTTP headers。 这非常有用,但由于许多组件也使用 headers 作为内部状态,外部 HTTP 调用可能会泄露内部信息。 例如,如果作业是使用计时器/cron 启动的,我们最终会在线上发送以下 HTTP headers:

http-outgoing-1 >> fireTime: Mon Dec 28 10:14:00 CET 2020 
http-outgoing-1 >> jobDetail: JobDetail 'Camel_camel-1.sync':  jobClass: 'org.apache.camel.component.quartz.CamelJob concurrentExectionDisallowed: false persistJobDataAfterExecution: false isDurable: false requestsRecovers: false 
http-outgoing-1 >> jobInstance: org.apache.camel.component.quartz.CamelJob@4ff90d52 
http-outgoing-1 >> jobRunTime: -1 
http-outgoing-1 >> mergedJobDataMap: org.quartz.JobDataMap@2338bfe0 
http-outgoing-1 >> nextFireTime: Mon Dec 28 10:15:00 CET 2020 
http-outgoing-1 >> refireCount: 0 
http-outgoing-1 >> scheduledFireTime: Mon Dec 28 10:14:00 CET 2020 
http-outgoing-1 >> scheduler: org.quartz.impl.StdScheduler@17a5f565 
http-outgoing-1 >> trigger: Trigger 'Camel_camel-1.syncJob':  triggerClass: 'org.quartz.impl.triggers.CronTriggerImpl calendar: 'null' misfireInstruction: 1 nextFireTime: Mon Dec 28 10:15:00 CET 2020 
http-outgoing-1 >> triggerGroup: Camel_camel-1 
http-outgoing-1 >> triggerName: syncJob 
... (actual needed HTTP headers:)
http-outgoing-1 >> Connection: Keep-Alive 
http-outgoing-1 >> User-Agent: Apache-HttpClient/4.5.12 (Java/11.0.6) 

我知道我可以在尝试 HTTP 调用之前删除所有 headers。 由于我的其他路由也添加了我需要的内部 headers,因此在发送呼叫之前为我删除所有“垃圾”headers 并不简单,但保留我的内部(也最后在 HTTP 调用中顺便说一句。)。

我知道我可以为此使用属性。 我知道我可以禁用 headers 消息自动添加为 HTTP headers。在这种情况下,我不确定是否需要手动添加 HTTP 所需的 headers(如 user-agent)。

headers 的其他常见误用是如果您进行了 2 次 HTTP 调用而忘记清除 headers, 第一次调用的输出 headers 将成为第一次调用的输入 headers。

有人找到了一个很好的解决方法来避免这个问题吗?

这就是 HTTP-based 端点(camel-http、camel-http4、camel-jetty、camel-restlet、camel-cxf、camel-cxfrs) 默认处理 headers(您可以使用 HeaderFilterStrategy 自定义此行为)。

  • Consumer:用CamelHttp*创建一个In消息headers记录传入消息的状态,所有的HTTP headers 来自原始消息,以及 URL 选项(仅限 Jetty)。

  • Producer:将Exchange it转换为目标消息格式CamelHttp* headers来控制HTTP生产者的行为端点 Camel* headers 被过滤掉,因为它们供内部使用,所有其他 headers 都转换为 HTTP headers,content-length, content-type, cache-control, connection, date, pragma, trailer, transfer-encoding, upgrade, via, warning 除外。

it is not simple to just remove all "junk" headers for me at point before sending the call, but keeping my internal ones (which also end up in the HTTP call btw.).

您可以在您的内部文件前加上 Camel 以避免将它们作为 HTTP headers 泄露。如果您需要其中一些作为 HTTP headers,您可以在调用之前手动映射到不同的键。

In this case I am not sure if I need to add manually the headers which are needed for HTTP (like user-agent).

你不需要它们。

Also other frequent misuse of the headers is if you make 2 HTTP calls and forget to clear the headers, the output headers of the first call will become input headers of the first.

在这种情况下,您至少应该删除控件 headers 和 .removeHeaders("CamelHttp*)