Search code examples
spring-integrationspring-integration-http

http:outbound-gateway to follow redirect


I need to request a JWT Token via an HTTP request in my Spring Integration application.

I've configured a plain http outbound gatway but the server replies with a 301 Moved Permanently;

It requires the client to follow a redirect (and apparently it works this way doing some tests with SOAP-UI);

How could I make the http-outbound-gateway to follow redirects?

Tried everything I could find, but nothing worked so far.

Thanks!


Solution

  • You need consider to configure your HTTP Outbound Gateway with a HttpComponentsClientHttpRequestFactory. This one is based on the Apache HTTP Client 4.x and its default behavior is a DefaultRedirectStrategy which does a redirect on GET and HEAD methods and when 302, 301 or 307 status is returned for the call.

    If you need to redirect POST, consider to configure an underlying HttpClient with a LaxRedirectStrategy.

    See more info here: Handling HttpClient Redirects

    UPDATE

    To configure a LaxRedirectStrategy for the HttpClient used in the HttpComponentsClientHttpRequestFactory you need something like this:

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

    It is kinda pitta to do all that stuff in XML, so consider to move your project to Java & annotation configuration.