我正在尝试使用Spring Integration进行HTTPS REST调用,下面是我的配置。
<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>
要解决这个问题,我要调用AllowAllHostnameVerifier,因为我正在调用内部REST服务。
CloseableHttpClient httpClient = HttpClients.custom().setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();
但是如何在我的spring配置xml中连接上面的java行?
答案 0 :(得分:2)
自定义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" />
答案 1 :(得分:1)
HttpClients.custom()
返回HttpClientBuilder
,因此只需将您自己的构建器bean和客户端bean替换为HttpClient
(在设置defaultRequestConfig
之后)。< / p>