|
@@ -0,0 +1,76 @@
|
|
|
+package com.chuanxia.mcp.sched;
|
|
|
+
|
|
|
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.ssl.SSLContexts;
|
|
|
+import org.apache.http.ssl.TrustStrategy;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.http.client.ClientHttpRequestFactory;
|
|
|
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.KeyStoreException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+
|
|
|
+/**
|
|
|
+ * restTemplate配置类
|
|
|
+ *
|
|
|
+ * @author Json
|
|
|
+ */
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class RestTemplateConfig {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RestTemplate restTemplate(ClientHttpRequestFactory factory) throws Exception {
|
|
|
+ TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
|
|
|
+
|
|
|
+ SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
|
|
|
+ .loadTrustMaterial(null, acceptingTrustStrategy)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
|
|
|
+
|
|
|
+ CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
+ .setSSLSocketFactory(csf)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
|
|
+
|
|
|
+ requestFactory.setHttpClient(httpClient);
|
|
|
+ requestFactory.setConnectionRequestTimeout(6000);
|
|
|
+ requestFactory.setConnectTimeout(6000);
|
|
|
+ requestFactory.setReadTimeout(6000);
|
|
|
+ return new RestTemplate(requestFactory);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
+ factory.setConnectTimeout(15000);
|
|
|
+ factory.setReadTimeout(5000);
|
|
|
+ return factory;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
|
|
|
+ throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException
|
|
|
+ {
|
|
|
+ TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
|
|
|
+ SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
|
|
|
+ SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
|
|
|
+
|
|
|
+ HttpClientBuilder httpClientBuilder = HttpClients.custom();
|
|
|
+ httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
|
|
|
+ CloseableHttpClient httpClient = httpClientBuilder.build();
|
|
|
+ HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
|
|
|
+ factory.setHttpClient(httpClient);
|
|
|
+ return factory;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|