Spring Integration XML 中用于 HTTPS 的 AllowAllHostnameVerifier [英] AllowAllHostnameVerifier for HTTPS in Spring Integration XML

查看:44
本文介绍了Spring Integration XML 中用于 HTTPS 的 AllowAllHostnameVerifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring Integration 进行 HTTPS REST 调用,以下是我的配置.

I am trying to make an HTTPS REST call using Spring Integration, below is my configuration.

<int-http:outbound-gateway
        id="Auth Outbound Gateway"
        request-channel="RequestChannel"
        request-factory="sslFactory"
        header-mapper="headerMapper"
        url="https://XX.XX.XX.XXX:XXXX/abcd"
        http-method="POST"
        expected-response-type="java.lang.String">
</int-http:outbound-gateway>

<bean id="sslFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
    <constructor-arg ref="httpClient"/>
</bean>

<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
      factory-method="custom">
    <property name="socketTimeout" value="10000" />
    <property name="connectTimeout" value="10000" />
</bean>

<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />

<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
      factory-method="create">
    <property name="defaultRequestConfig" ref="requestConfig" />
</bean>

<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />

我收到以下错误...

javax.net.ssl.SSLException: hostname in certificate didn't match: <XX.XX.XX.XXX> != <abc.abc.xyz.com>

为了解决这个问题,我不想在调用内部 REST 服务时使用 AllowAllHostnameVerifier.

To solve this I wan't to AllowAllHostnameVerifier as I am calling to internal REST services.

CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();

但是如何在我的 spring 配置 xml 中连接上面的 java 行?

but how do I wire the above java line in my spring configuration xml?

推荐答案

自定义 Java

public class HttpClientFactory extends AbstractFactoryBean<HttpClient> {

    @Override
    public Class<?> getObjectType() {
        return HttpClient.class;
    }

    @Override
    protected HttpClient createInstance() throws Exception {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        TrustStrategy allTrust = new TrustStrategy() {
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }
        };

        SSLContext sslcontext = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, allTrust).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

        return httpClient;
    }
}

XML 配置

<!--SSL-->
<bean id="sslFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
    <constructor-arg ref="httpClient"/>
</bean>
<bean id="httpClient" class="org.springframework.integration.samples.http.HttpClientFactory" />

这篇关于Spring Integration XML 中用于 HTTPS 的 AllowAllHostnameVerifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆