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

Spring integration 如何将自定义头添加到HTTP出站网关到安全REST服务,spring-integration,Spring Integration,我的spring集成项目配置如下: 1-使用MQ XML消息的JMS消息驱动通道适配器 2-HTTP出站网关,用于将这些XML消息发送到安全的REST服务 3-REST服务需要在HTTP请求头中设置身份验证令牌 为了完成#3,我在配置中添加了一个出站网关前面的header enricher组件 ... --> DirectChannel --> Header-enricher --> DirectChannel --> HTTP outbound-gateway --&g

我的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>

使用
映射的请求头
-请参阅


为了完成Gary的回答,在创建请求时只映射标准HTTP头。要添加任何自定义标头,如果标头以“X-”开头,则可以使用“映射的请求标头”,或者指定自己的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_请求_标头)。出站网关可以像这样使用您的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"> 


非常感谢加里的帮助。
    <bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, My-Token" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>
<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">