public class CustomHttp4Component { @Produces @Named("insecurehttps4") public HttpComponent createCustomHttp4Component() { HttpComponent httpComponent = new HttpComponent();
In this custom http4 component we have to do two things. First is to change the Certificate HostnameVerifier so the hostname on the self-signed certificate is not causing an exception.
This is accomplished by setting an instance of the org.apache.http.conn.ssl.AllowAllHostnameVerifier to the http4 component using the setter method setX509HostNameVerifier. Note, the version of Apache Camel I am using (2.17) still requires the now deprecated: org.apache.http.conn.ssl.AllowAllHostnameVerifier newer versions of Apache Camel are using the org.apache.http.conn.ssl.NoopHostnameVerifier.
This unfortunately was not enough to invoke the endpoint, an empty X509TrustManager was also required. It needs to be empty for our purpose to basically omit the certificate validation checks. For this we needed to extend the X509ExtendedTrustManager and override the methods implementing them as “empty”. To set our empty thrustmanager on the http4 component we need to wrap it in a TrustManagersParameters class and wrap this into an SSLParameters class before we can add it to the http4 component.
TrustManagersParameters trustManagersParameters = new TrustManagersParameters(); X509ExtendedTrustManager extendedTrustManager = new InsecureX509TrustManager(); trustManagersParameters.setTrustManager(extendedTrustManager); SSLContextParameters sslContextParameters = new SSLContextParameters(); sslContextParameters.setTrustManagers(trustManagersParameters); httpComponent.setSslContextParameters(sslContextParameters);
package nl.schiphol.api.integration.components; import org.apache.camel.component.http4.HttpComponent; import org.apache.camel.util.jsse.SSLContextParameters; import org.apache.camel.util.jsse.TrustManagersParameters; import org.apache.http.conn.ssl.AllowAllHostnameVerifier; import javax.enterprise.inject.Produces; import javax.inject.Named; import javax.net.ssl.SSLEngine; import javax.net.ssl.X509ExtendedTrustManager; import java.net.Socket; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class CustomHttp4Component { @Produces @Named("insecurehttps4") public HttpComponent createCustomHttp4Component() { HttpComponent httpComponent = new HttpComponent(); httpComponent.setX509HostnameVerifier(AllowAllHostnameVerifier.INSTANCE); TrustManagersParameters trustManagersParameters = new TrustManagersParameters(); X509ExtendedTrustManager extendedTrustManager = new InsecureX509TrustManager(); trustManagersParameters.setTrustManager(extendedTrustManager); SSLContextParameters sslContextParameters = new SSLContextParameters(); sslContextParameters.setTrustManagers(trustManagersParameters); httpComponent.setSslContextParameters(sslContextParameters); return httpComponent; } private static class InsecureX509TrustManager extends X509ExtendedTrustManager { @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }