Question

How to migration ssl connection from Apache httpClient 4.2.6 to httpclient 4.5.14 in bean xml (Spring application)

We are migrating from Apache httpClient 4.2.6 to Apache httpClient 4.5.14. We are creating beans using bean xml in Spring boot application. Many classes and methods are deprecated. In below snippet Scheme, SSLSocketFactory and SchemeRegistryFactory are deprecated.


   <beans>
      <bean id="RAM.SSLSocketFactoryBean" class="org.apache.http.conn.ssl.SSLSocketFactory">
         <property name="enabledSSLProtocols" value="${RAM.enabledSSLProtocols}" />
         <property name="trustStoreLocation" value="${csi.adapter.projecthomepath}/${RAM.truststorelocation}" />
         <property name="trustStorePassword" value="${RAM.truststore.password}" />
         <property name="publicKeyAliasList" value="${RAM.publickey.alias.list}" />
         <property name="enabledCiphers" value="${RAM.enabledCiphers}" />
      </bean>
      <bean id="RAM.Scheme" class="org.apache.http.conn.scheme.Scheme">
         <constructor-arg index="0" value="https" />
         <constructor-arg index="1" value="443" />
         <constructor-arg index="2" ref="RAM.SSLSocketFactoryBean" />
      </bean>
      <bean id="RAM.SchemeRegistry" class="org.springframework.beansfactory.config.MethodInvokingFactoryBean">
         <property name="targetClass">
            <value>org.apache.http.impl.conn.SchemeRegistryFactory</value>
         </property>
         <property name="targetMethod">
            <value>createDefault</value>
         </property>
      </bean>
      <bean class="erg.springframework.beans.factory.config.MethodInvokingFactoryBean" lazy-init="false">
         <property name="targetObject">
            <ref bean="RAM.SchemeRegistry" />
         </property>
         <property name="targetMethod">
            <value>register</value>
         </property>
         <property name="arguments">
            <list>
               <ref bean="RAM.Scheme" />
            </list>
         </property>
      </bean>
   </beans>

I dont know how to change these beans to latest httpClient 4.5.14. Please help me.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
January 12, 2024

Hi there,

Migrating your Spring application’s configuration from Apache HttpClient 4.2.6 to 4.5.14 involves updating the beans to use the new classes and methods provided in the newer version.

Since Apache HttpClient 4.5.x has deprecated some of the classes like Scheme and SSLSocketFactory, you need to use the SSLContextBuilder and RegistryBuilder for setting up SSL connections:

https://hc.apache.org/httpcomponents-client-4.5.x/index.html

Here’s a general idea on how to refactor your bean configuration in XML to be compatible with HttpClient 4.5.14:

  1. SSLSocketFactory Replacement: Use SSLContextBuilder to create an SSLContext, and then create an SSLConnectionSocketFactory from the SSLContext.

  2. Scheme and SchemeRegistry Replacement: Instead of Scheme and SchemeRegistry, use RegistryBuilder to create a Registry<ConnectionSocketFactory>.

  3. HttpClient Creation: Finally, create the HttpClient using HttpClientBuilder and set the Registry<ConnectionSocketFactory>.

Here’s a revised version of your beans configuration:

<beans>
    <!-- SSLContext configuration -->
    <bean id="sslContext" class="org.apache.http.ssl.SSLContextBuilder" factory-method="create">
        <property name="loadTrustMaterial" ref="trustStore" />
        <!-- Add other SSL configurations if needed -->
    </bean>

    <bean id="trustStore" class="org.apache.http.ssl.TrustSelfSignedStrategy"/>

    <bean id="sslSocketFactory" class="org.apache.http.conn.ssl.SSLConnectionSocketFactory">
        <constructor-arg ref="sslContext"/>
        <property name="supportedProtocols" value="${RAM.enabledSSLProtocols}"/>
        <property name="supportedCipherSuites" value="${RAM.enabledCiphers}"/>
    </bean>

    <!-- Registry for connection socket factories -->
    <bean id="registry" class="org.apache.http.config.RegistryBuilder" factory-method="create">
        <property name="register">
            <list>
                <bean class="org.apache.http.config.RegistryBuilder$SchemeRegistryBuilder">
                    <constructor-arg value="https"/>
                    <constructor-arg value="443"/>
                    <constructor-arg ref="sslSocketFactory"/>
                </bean>
            </list>
        </property>
    </bean>

    <!-- HttpClient creation -->
    <bean id="httpClient" class="org.apache.http.impl.client.HttpClients" factory-method="custom">
        <property name="setDefaultSocketConfig">
            <!-- Configure socket config if needed -->
        </property>
        <property name="setConnectionManager">
            <bean class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
                <constructor-arg ref="registry"/>
            </bean>
        </property>
        <!-- Add other configurations as needed -->
    </bean>
</beans>

In this configuration:

  • The SSLContextBuilder is used to create an SSLContext.
  • TrustSelfSignedStrategy is an example; you may need to implement your trust strategy based on your application needs.
  • SSLConnectionSocketFactory is created with the SSLContext.
  • A Registry of connection socket factories is created.
  • The HttpClient is built with the custom configurations.

Ensure that all the placeholders like ${RAM.enabledSSLProtocols} are correctly configured in your properties file.

Also, make sure to always test thoroughly after making these changes to ensure that your application’s SSL connections are functioning as expected with the new HttpClient version.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.