当前位置: 首页 > 知识库问答 >
问题:

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

楚煜
2023-03-14

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

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

2- 用于将这些 XML 消息发送到受保护的 REST 服务的 HTTP 出站网关。

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>

日志显示“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

共有2个答案

都阳辉
2023-03-14

为了完成 Gary 的答案,在创建请求时,将仅映射标准的 HTTP 标头。要添加任何自定义标头,您可以使用“映射请求标头”(如果标头以“X-”开头),也可以指定自己的默认HttpHeader映射器,如下所示:

    <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"> 
姚宪
2023-03-14

使用映射的请求标头 - 请参阅文档部分“HTTP 标头映射”。

mapped-request-headers="HTTP_REQUEST_HEADERS, X-My-Token"
 类似资料:
  • 我正在尝试使用< code >传递一些自定义的HTTP头 我试过两种配置。第一个具有“< code > mapped-request-headers ”属性: 第二个具有“”属性,具有相对配置: 但在这两种情况下,远程应用程序加载的消息头如下: 没有我试图添加到请求消息中的“test”头的跟踪。 向 中? 事实上,标题似乎在GenericMessage中“消失”了: 我不明白为什么HTTP请求中会

  • 我正试图发送一个授权令牌,但我的服务器不知何故没有接收它。 //service.ts } //endpoint //标记过滤器

  • 问题内容: 澄清点:将自定义标头添加到我的jQuery ajax调用中没有任何问题,我想将我的自定义标头自动添加到所有ajax调用中。 如果您看看jquery $.ajax自定义http标头问题(不是我的问题),您将看到一个很好的示例,说明如何为每个ajax调用手动实现代码。 我想为所有jQuery ajax调用覆盖beforeSend。根据jQuery文档,我可以使用jQuery.ajaxSet

  • 我使用Dropwizard Metrics library记录我的应用程序中各种操作的时间,使用计时器和控制台报告器,并将计数、平均速率等记录到控制台fine。 我希望这些数字也可以在 /metricsservlet上使用,并且基于http://metrics.dropwizard.io/3.1.0/manual/servlets/我需要访问名为的Metric注册表。但是查看文档和代码,我看不到是

  • 问题内容: 我需要向来自WebView的每个请求添加自定义标头。我知道有用于的参数,但这些参数仅应用于初始请求。所有后续请求均不包含标头。我已经看过所有的替代,但是没有什么允许将标头添加到资源请求- 。任何帮助都会很棒。 问题答案: 尝试 要将标头添加到资源加载请求中,请定制WebViewClient并重写:

  • 问题内容: 这是我的第一篇文章。 我刚刚开始学习Go和Angular,并且尝试将angular应用程序连接到go api。我已经写了这两本书,并且一直坚持找出问题的根源。我以为这是一个CORS问题,但是如果我在Angular http请求中不包含代码的标题行,它就可以正常工作。在这一点上,我只是想添加标题。授权代码尚未实现。 这两个应用程序都在本地运行,端口5000上的Go应用程序和4200端口上