【问题标题】:http:outbound-gateway to follow redirecthttp:outbound-gateway 跟随重定向
【发布时间】:2020-03-03 10:42:11
【问题描述】:

我需要在 Spring Integration 应用程序中通过 HTTP 请求请求 JWT 令牌。

我配置了一个普通的 http 出站网关,但服务器回复 301 Moved Permanently;

它要求客户端遵循重定向(显然它以这种方式使用 SOAP-UI 进行一些测试);

如何使 http-outbound-gateway 跟随重定向?

尝试了我能找到的一切,但到目前为止没有任何效果。

谢谢!

【问题讨论】:

    标签: spring-integration spring-integration-http


    【解决方案1】:

    您需要考虑使用HttpComponentsClientHttpRequestFactory 配置您的HTTP 出站网关。这是基于 Apache HTTP Client 4.x 的,它的默认行为是 DefaultRedirectStrategy,它在 GETHEAD 方法上进行重定向,并且当返回 302301307 状态时来电。

    如果您需要重定向POST,请考虑使用LaxRedirectStrategy 配置底层HttpClient

    在此处查看更多信息:Handling HttpClient Redirects

    更新

    要为HttpComponentsClientHttpRequestFactory 中使用的HttpClient 配置LaxRedirectStrategy,您需要这样的东西:

    <beans:bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClients" factory-method="custom">
        <beans:property name="redirectStrategy" value="#{new org.apache.http.impl.client.LaxRedirectStrategy()}"/>
    </beans:bean>
    
    <beans:bean id="clientHttpRequestFactory"
          class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
        <beans:constructor-arg>
            <beans:bean factory-bean="httpClientBuilder" factory-method="build"/>
        </beans:constructor-arg>
    </beans:bean>
    

    在 XML 中做所有这些事情有点麻烦,所以考虑将您的项目移动到 Java 和注释配置。

    【讨论】:

    • 感谢 Artem,效果很好!不幸的是,我尝试使用的服务还有其他问题,但这不是 SI 错误:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-05
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2023-03-12
    • 2012-05-04
    • 1970-01-01
    相关资源
    最近更新 更多