【发布时间】:2015-09-13 00:11:40
【问题描述】:
我的spring集成项目配置如下:
1- 使用 MQ XML 消息的 JMS 消息驱动通道适配器。
2- HTTP 出站网关将这些 XML 消息发送到安全的 REST 服务。
3- REST 服务需要在 HTTP 请求标头中设置身份验证令牌。
为了完成 #3,我在出站网关前向我的配置中添加了一个 header-enricher 组件。
... --> DirectChannel --> Header-enricher --> DirectChannel --> HTTP outbound-gateway --> ...
我遇到的问题是使用标头中包含的令牌进行 REST 服务请求调用。因此我收到 401 错误。
<int-http:outbound-gateway
url="${outbound.rest.url}"
request-channel="httpOutboundRequestChannel"
reply-channel="httpOutboundReplyChannel"
http-method="POST"
expected-response-type="java.lang.String">
</int-http:outbound-gateway>
<int:header-enricher input-channel="httpHeaderEnricherChannel" output-channel="httpOutboundRequestChannel">
<int:header name="Content-Type" value="application/xml"/>
<int:header name="X-My-Token" value="mytokenvaluehere"/>
</int:header-enricher>
日志显示“X-My-Token”标头已添加到消息中,但未添加到出站网关的请求中。
如何将自定义标头添加到出站网关组件?
非常感谢任何建议!
日志:
20656 [task-scheduler-4] DEBUG org.springframework.integration.channel.DirectChannel
preSend on channel 'httpOutboundRequestChannel', message: GenericMessage [payload=my XML goes here, headers={JMS_IBM_Character_Set=UTF-8, JMS_IBM_MsgType=8, JMSXUserID=someid , JMS_IBM_Encoding=273, priority=4, jms_timestamp=1441991857194, JMSXAppID=WebSphere MQ Client for Java, JMS_IBM_PutApplType=28, JMS_IBM_Format=MQSTR , jms_redelivered=false, JMS_IBM_PutDate=20150911, JMSXDeliveryCount=1, X-Auth-Token=mytokenvaluehere, JMS_IBM_PutTime=17173719, id=ffd41297-8320-912a-1bac-2dca14bb658a, jms_messageId=ID:414d512054315055434b2020202020205afce255021cf422, Content-Type=application/xml, timestamp=1441991857282}]
20656 [task-scheduler-4] DEBUG org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandlerorg.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#0 received message:
GenericMessage [payload=my XML goes here, headers={JMS_IBM_Character_Set=UTF-8, JMS_IBM_MsgType=8, JMSXUserID=someid , JMS_IBM_Encoding=273, priority=4, jms_timestamp=1441991857194, JMSXAppID=WebSphere MQ Client for Java, JMS_IBM_PutApplType=28, JMS_IBM_Format=MQSTR , jms_redelivered=false, JMS_IBM_PutDate=20150911, JMSXDeliveryCount=1, X-My-Token=mytokenvaluehere, MS_IBM_PutTime=17173719, id=ffd41297-8320-912a-1bac-2dca14bb658a, jms_messageId=ID:414d512054315055434b2020202020205afce255021cf422, Content-Type=application/xml, timestamp=1441991857282}]
20806 [task-scheduler-4] DEBUG org.springframework.web.client.RestTemplate
Created POST request for "https://my.rest.uri.here"
20809 [task-scheduler-4] DEBUG org.springframework.web.client.RestTemplate
Setting request Accept header to [text/plain, application/json, application/*+json, */*]
20810 [task-scheduler-4] DEBUG org.springframework.web.client.RestTemplate Writing [my XML goes here] as "application/xml" using [org.springframework.http.converter.StringHttpMessageConverter@1269bf3]
21413 [task-scheduler-8] DEBUG org.springframework.integration.endpoint.PollingConsumer Received no Message during the poll, returning 'false'
21524 [task-scheduler-4] DEBUG org.springframework.web.client.RestTemplate
POST request for "https://my.rest.uri.here" resulted in 401 (Unauthorized); invoking error handler
【问题讨论】: