首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:如何向HTTP出站网关添加自定义HTTP报头?

:如何向HTTP出站网关添加自定义HTTP报头?
EN

Stack Overflow用户
提问于 2016-07-20 10:36:34
回答 2查看 7.7K关注 0票数 0

我试图使用Spring的<int-http:outbound-gateway><int:header-enricher>组件传递一些自定义的headers,但是它在我的配置中不起作用。

我试过两种配置。第一个属性为"mapped-request-headers“:

代码语言:javascript
复制
<int:chain input-channel="requestChannel" output-channel="replyChannel">
    <int:header-enricher>
        <int:header name="test" value="test"></int:header>
    </int:header-enricher>
    <int-http:outbound-gateway id="gateway"
        encode-uri="true" url="http://localhost:8080/webapp/service/{request}"
        http-method="GET" mapped-request-headers="test, HTTP_REQUEST_HEADERS">
        <int-http:uri-variable name="request" expression="payload"/>
    </int-http:outbound-gateway>
</int:chain>

第二个属性为"header-mapper“,具有相对的DefaultHttpHeaderMapper配置:

代码语言:javascript
复制
<int:chain input-channel="requestChannel" output-channel="replyChannel">
    <int:header-enricher>
        <int:header name="test" value="test"></int:header>
    </int:header-enricher>
    <int-http:outbound-gateway id="gateway"
        encode-uri="true" url="http://localhost:8080/webapp/service/{request}"
        http-method="GET" header-mapper="headerMapper">
        <int-http:uri-variable name="request" expression="payload"/>
    </int-http:outbound-gateway>
</int:chain>

<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, test" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>

但在这两种情况下,远程应用程序加载的消息头如下所示:

代码语言:javascript
复制
GenericMessage[
    payload={},
    headers={
        http_requestMethod=GET,
        replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4c8b7a27,
        errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@4c8b7a27,
        host=localhost: 8080,
        http_requestUrl=http://localhost:8080/webapp/service/hello,
        connection=keep-alive,
        id=3c2a21ba-b7f5-e5e9-c821-45251d406318,
        cache-control=no-cache,
        pragma=no-cache,
        user-agent=Java/1.8.0_65,
        accept=[
            text/html,
            image/gif,
            image/jpeg,
            */*;q=.2,
            */*;q=.2],
        timestamp=1469009855797
    }
]

没有跟踪我试图添加到请求消息中的"test“标头

<int-http:outbound-gateway>添加自定义头的正确方法是什么?

这也是很好的任何其他工作解决方案。

更新

从Gary指示,我打开了调试模式。

这是一件非常有趣的事情:日志显示头测试“将映射”

代码语言:javascript
复制
message sent: GenericMessage [payload=hello, headers={id=79f98422-418b-f00f-1e21-09461b1ff80c, timestamp=1469021452494}]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.fromHeaders:402 (executor-4) - outboundHeaderNames=[test, HTTP_REQUEST_HEADERS]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.shouldMapHeader:537 (executor-4) - headerName=[test] WILL be mapped, matched pattern=test
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.fromHeaders:420 (executor-4) - setting headerName=[X-test], value=test
2016-07-20 15:53:57 DEBUG org.springframework.http.client.support.HttpAccessor.createRequest:79 (executor-4) - Created GET request for "http://localhost:8080/webapp/service/hello"

我在服务器端实现了一个简单的过滤器,它拦截每个HTTP请求,并在日志上打印该请求的每个标头。

其结果是:

代码语言:javascript
复制
x-test=test
cache-control=no-cache
pragma=no-cache
user-agent=Java/1.8.0_65
host=localhost:8080
accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
connection=keep-alive

因此,头"x-test"出现在HttpServletRequest实例上。

但之后,我可以在日志中看到另一条消息(我只报告了对此上下文具有重要意义的消息):

代码语言:javascript
复制
2016-07-20 15:53:57 DEBUG org.springframework.web.servlet.DispatcherServlet.doService:865 (http-nio-8080-exec-1) - DispatcherServlet with name 'dispatcherServer' processing GET request for [/webapp/service/hello]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.toHeaders:436 (http-nio-8080-exec-1) - inboundHeaderNames=[Accept, Accept-Charset, Accept-Encoding, Accept-Language, Accept-Ranges, Authorization, Cache-Control, Connection, Content-Length, Content-Type, Cookie, Date, Expect, From, Host, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Max-Forwards, Pragma, Proxy-Authorization, Range, Referer, TE, Upgrade, User-Agent, Via, Warning]
2016-07-20 15:53:57 DEBUG org.springframework.integration.http.support.DefaultHttpHeaderMapper.shouldMapHeader:561 (http-nio-8080-exec-1) - headerName=[x-test] WILL NOT be mapped
2016-07-20 15:53:57 DEBUG org.springframework.integration.handler.AbstractMessageHandler.handleMessage:115 (executor-4) - ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@6ae60a] (channelServiceActivator) received message: GenericMessage [payload={}, headers={http_requestMethod=GET, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@565fe0d8, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@565fe0d8, host=localhost:8080, http_requestUrl=http://localhost:8080/webapp/service/hello, connection=keep-alive, id=9578f82d-7fc2-fe94-d8bf-3225ec955b22, cache-control=no-cache, pragma=no-cache, user-agent=Java/1.8.0_65, accept=[text/html, image/gif, image/jpeg, */*;q=.2, */*;q=.2], timestamp=1469022837172}]

现在日志上说,头"x-test将不会被映射“。怎么可能?因为它没有出现在inboundHeaderNames中?

实际上,标题似乎在GenericMessage中“消失了”:

代码语言:javascript
复制
GenericMessage [
    payload={}, 
    headers={
        http_requestMethod=GET,
        replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@16b10fb6,
        errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@16b10fb6,
        host=localhost:8080,
        http_requestUrl=http://localhost:8080/webapp/service/hello,
        connection=keep-alive,
        id=ba2af515-9e29-4d84-d176-5b9d0d066cb0,
        cache-control=no-cache,
        pragma=no-cache,
        user-agent=Java/1.8.0_65,
        accept=[text/html,
            image/gif,
            image/jpeg,
            */*;q=.2,
            */*;q=.2],
        timestamp=1469021452542
    }
]

我不明白为什么当前的头在HTTP请求中,但它不存在于消息通道的标题中。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-20 15:24:02

我已经找到了这个问题的答案。

<int-http:outbound-gateway>映射必须插入到“退出” HTTP请求中的标头。

<int-http:inbound-gateway>映射包含在“输入” HTTP请求中的标头。

因此,必须定义一个"header-mapper",即:

  • 映射用于"inboundHeaderNames"<int-http:inbound-gateway>
  • 映射用于"outboundHeaderNames"<int-http:outbound-gateway>

在我的示例中,“出站端”(客户端应用程序)的解决方案是:

代码语言:javascript
复制
<int:chain input-channel="requestChannel" output-channel="replyChannel">
    <int:header-enricher>
        <int:header name="test" value="test"></int:header>
    </int:header-enricher>
    <int-http:outbound-gateway id="gateway"
        encode-uri="true" url="http://localhost:8080/webapp/service/{request}"
        http-method="GET" header-mapper="headerMapper">
        <int-http:uri-variable name="request" expression="payload"/>
    </int-http:outbound-gateway>
</int:chain>

<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, test" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>

"outboundHeaderNames"必须使用必须映射到“出站请求”(在本例中为“test”)中映射的头名称来设置。

“入站端”(接收方应用程序)的解决方案是:

代码语言:javascript
复制
<int-http:inbound-gateway id="gateway" request-channel="requestChannel"
      path="/service/**" supported-methods="GET"
      reply-channel="outputChannel" header-mapper="headerMapper">
</int-http:inbound-gateway>

<bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*" />
</bean>

设置为"*"的确保在消息头中映射源请求的所有头部。

使用这种方法,我们可以将自定义头传递给HTTP请求中的任何URL,并且可以在消息通道上下文中的服务器端恢复它们。

票数 3
EN

Stack Overflow用户

发布于 2016-07-20 12:49:37

打开调试日志;您应该看到有关头映射的消息,如下所示.

08:46:44.987调试主.headerName=id不会被映射 08:46:44.987调试主.headerName=test将被映射,匹配的pattern=test 08:46:44.987调试主.设置headerName=X-test,value=foo

(这是在您的第一个示例中-自定义头当前默认获得X-前缀)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38478936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档