【问题标题】:How to add custom header to HTTP outbound-gateway to a secured REST service如何将自定义标头添加到 HTTP 出站网关到安全的 REST 服务
【发布时间】: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

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢 Gary 的帮助。
    【解决方案2】:

    要完成 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>
    

    需要为要获取的自定义标头(默认为“X-”,因此不会映射其他标头)以及标准标头(HTTP_REQUEST_HEADERS)定义HeaderPrefix。出站网关可以像这样使用你的 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"> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 2012-04-23
      • 1970-01-01
      • 2015-04-05
      相关资源
      最近更新 更多