Spring Integration HTTP 出站网关标头不是连续请求的转发器 [英] Spring Integration HTTP Outbound Gateway header not forwarder on a consecutive request

查看:32
本文介绍了Spring Integration HTTP 出站网关标头不是连续请求的转发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下流程中挣扎:

.enrichHeaders(h -> h.headerFunction("ocp-apim-subscription-key", m ->
        "xxx"))
.handle(Http.outboundGateway("https://northeurope.api.cognitive.microsoft.com/vision/v3" +
        ".0/read/analyzeResults/abc")
        .mappedRequestHeaders("ocp-apim-subscription-key")
        .httpMethod(HttpMethod.GET))
.enrichHeaders(h -> h.headerFunction("ocp-apim-subscription-key", m ->
        "xxx"))
.handle(Http.outboundGateway("https://northeurope.api.cognitive.microsoft.com/vision/v3" +
        ".0/read/analyzeResults/def")
        .mappedRequestHeaders("ocp-apim-subscription-key")
        .httpMethod(HttpMethod.GET))

第一个请求被正确提交,我得到了结果,对于第二个我得到 401 UNAUTHORIZED 这意味着,ocp-apim-subscription-key 不是包括在内.我试过没有第二个浓缩步骤,因为我认为标题不会被清除,但它也没有改变任何东西.

The first request is submitted correctly and I get the result, for the second one I get 401 UNAUTHORIZED which means, the ocp-apim-subscription-key is not included. I've tried without the second enrichment step as I thought that the headers won't be cleared but it also didn't change anything.

知道我做错了什么吗?我是否需要以不同的方式配置标头映射器?

Any idea what I might be doing wrong? Do I need to configure the header mapper somehow differently?

这是调试的输出,清楚地表明包含标题:

Here is the output of the debug which clearly shows that the header is included:

17:45:31.468 [main] DEBUG org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler - bean 'ocrDocument.http:outbound-gateway#2' for component 'ocrDocument.org.springframework.integration.config.ConsumerEndpointFactoryBean#3'; defined in: 'processing.OCRIntegrationFlow'; from source: 'bean method ocrDocument' received message: GenericMessage [payload=<200,[Transfer-Encoding:"chunked", Content-Type:"application/json; charset=utf-8", x-envoy-upstream-service-time:"25", CSP-Billing-Usage:"CognitiveServices.ComputerVision.Transaction=1", Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", x-content-type-options:"nosniff", Date:"Mon, 31 Aug 2020 15:45:31 GMT"]>, headers={Transfer-Encoding=chunked, ocp-apim-subscription-key=xxx, id=11fa4a77-d97a-772b-69b6-059de29ef808, contentType=application/json;charset=utf-8, http_statusCode=200 OK, Date=1598888731000, timestamp=1598888731467}]

更新我用wireshark录制了一个会话(切换到http而不是https,因为我无法让它工作).似乎在第二个请求中没有传播订阅密钥.出于某种原因,在第二个中包含了更多标题.

UPDATE I've recorded a session with wireshark (switched to http instead of https as I couldn't get it to work). It seems that in the second request the subscription-key isn't propagated. For some reason in the second one more headers are included.

第一个

第二个

推荐答案

好的.我知道问题出在哪里:

OK. I see where is the problem:

private HttpEntity<?> createHttpEntityFromPayload(Message<?> message, HttpMethod httpMethod) {
Object payload = message.getPayload();
if (payload instanceof HttpEntity<?>) {
    // payload is already an HttpEntity, just return it as-is
    return (HttpEntity<?>) payload;
}
HttpHeaders httpHeaders = mapHeaders(message);

由于您将 ResponseEntity 从第一次调用传播到第二次调用,因此确实没有任何标头映射,因为我们只是没有在 AbstractHttpRequestExecutingMessageHandler 中执行该逻辑并按原样使用提供的 HttpEntity.

Since you propagate a ResponseEntity from the first call to the second one, there is indeed no any headers mapping since we just don't do that logic in the AbstractHttpRequestExecutingMessageHandler and use the provided HttpEntity as is.

我们无法假设您想用它做什么,但由于您提供了整个实体,我们只是不对其进行变异并按原样对其执行请求.

We can't make an assumption what you would like to do with that, but since you have provided the whole entity, we just don't mutate it and perform request against it as is.

为了解决这个问题,我建议在第二次调用之前包含一些简单的 .transform((p) -> "") 以避免一些 HTTP 实体假设.

To fix the problem I suggest to include before second call some simple .transform((p) -> "") to avoid some HTTP entity assumption.

是的,如果 ocp-apim-subscription-key 的值相同,则您不需要第二个标头丰富器.

And yes, you don't need the second header enricher if the value for the ocp-apim-subscription-key is the same.

我们可能需要改进关于此事的文档并解释如何在此组件中处理请求消息.随时提出 GH 问题!

We probably need to improve docs on the matter and explain how request message is handled in this component. Feel free to raise a GH issue!

这篇关于Spring Integration HTTP 出站网关标头不是连续请求的转发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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