如何将自定义标头添加到HTTP出站网关到安全的REST服务

时间:2015-09-13 00:11:41

标签: spring-integration

我的spring集成项目配置如下:

1- JMS消息驱动通道适配器,用于使用MQ XML消息。

2- HTTP出站网关,用于将这些XML消息发送到安全的REST服务。

3- REST服务需要在HTTP请求标头中设置身份验证令牌。

为了完成#3,我在出站网关前面添加了一个包含头部的组件。

... --> 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>

日志显示&#34; X-My-Token&#34;标头正在添加到消息中,但不会添加到出站网关的请求中。

如何将自定义标头添加到出站网关组件?

非常感谢任何建议!

日志:

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

2 个答案:

答案 0 :(得分:1)

使用mapped-request-headers - 请参阅documentation section 'HTTP Header Mappings'

mapped-request-headers="HTTP_REQUEST_HEADERS, X-My-Token"

答案 1 :(得分:0)

要完成Gary的回答,在创建请求时只会映射标准HTTP标头。要添加任何自定义标头,如果标头以“X-”开头,您可以使用“mapped-request-headers”,也可以按如下方式指定自己的DefaultHttpHeaderMapper:

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

需要为要拾取的自定义标头定义HeaderPrefix(默认为'X-',因此不会映射其他标头)以及标准标头(HTTP_REQUEST_HEADERS)。出站网关可以使用你的headerMapper:

<int-http:outbound-gateway 
    url="${outbound.rest.url}" 
    request-channel="httpOutboundRequestChannel" 
    reply-channel="httpOutboundReplyChannel" 
    http-method="POST"
    header-mapper="headerMapper"
    expected-response-type="java.lang.String">