|
@@ -0,0 +1,186 @@
|
|
|
|
|
+package com.shkpr.service.warncore.configuration;
|
|
|
|
|
+
|
|
|
|
|
+import okhttp3.Interceptor;
|
|
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
|
|
+import okhttp3.Request;
|
|
|
|
|
+import okhttp3.Response;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
|
|
+import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
|
|
|
|
+import org.springframework.http.converter.HttpMessageConverter;
|
|
|
|
|
+import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
+
|
|
|
|
|
+import javax.net.ssl.*;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.net.InetSocketAddress;
|
|
|
|
|
+import java.net.Proxy;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.security.KeyManagementException;
|
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
|
+import java.security.SecureRandom;
|
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * OkHttp3特别注意:1) 所有的header不能设置null
|
|
|
|
|
+ * 2) 需要显示的设置user-agent请求头
|
|
|
|
|
+ * 3) 需要关闭人工的Accept-Encoding:gzip压缩解压机制,OkHttp3会默认启用自身的压缩解压机制
|
|
|
|
|
+ */
|
|
|
|
|
+@Configuration
|
|
|
|
|
+@EnableAutoConfiguration
|
|
|
|
|
+public class OkHttpClientConfig {
|
|
|
|
|
+ @Value("${global.www.proxy.address:}")
|
|
|
|
|
+ private String strWWWProxyAddress = "";
|
|
|
|
|
+
|
|
|
|
|
+ @Primary
|
|
|
|
|
+ @Bean(name = "RestTemplateEx")
|
|
|
|
|
+ public RestTemplate restTemplate(@Qualifier("OkHttpClient") OkHttpClient okHttpClient) {
|
|
|
|
|
+ OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate(factory);
|
|
|
|
|
+ // 可以添加消息转换
|
|
|
|
|
+ //restTemplate.setMessageConverters(...);
|
|
|
|
|
+ // 可以增加拦截器
|
|
|
|
|
+ //restTemplate.setInterceptors(...);
|
|
|
|
|
+ return restTemplate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Primary
|
|
|
|
|
+ @Bean(name = "RestTemplateUTF8")//解决body无法进行UTF-8编码时的中文乱码问题,如:APPLICATION_XML默认无法指定UTF-8编码;而APPLICATION_JSON_UTF8默认就对body进行了UTF-8编码
|
|
|
|
|
+ public RestTemplate restTemplateUTF8(@Qualifier("OkHttpClient") OkHttpClient okHttpClient) {
|
|
|
|
|
+ OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate(factory);
|
|
|
|
|
+ List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
|
|
|
|
|
+ if (messageConverters != null){
|
|
|
|
|
+ for (int i = 0; i < messageConverters.size(); i++) {
|
|
|
|
|
+ HttpMessageConverter<?> httpMessageConverter = messageConverters.get(i);
|
|
|
|
|
+ if (httpMessageConverter.getClass().equals(StringHttpMessageConverter.class)) {
|
|
|
|
|
+ messageConverters.set(i, new StringHttpMessageConverter(StandardCharsets.UTF_8));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return restTemplate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "RestTemplateProxy")
|
|
|
|
|
+ public RestTemplate restTemplateProxy(@Qualifier("OkHttpClientProxy") OkHttpClient okHttpClient) {
|
|
|
|
|
+ OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate(factory);
|
|
|
|
|
+ // 可以添加消息转换
|
|
|
|
|
+ //restTemplate.setMessageConverters(...);
|
|
|
|
|
+ // 可以增加拦截器
|
|
|
|
|
+ //restTemplate.setInterceptors(...);
|
|
|
|
|
+ return restTemplate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "OkHttpClient")
|
|
|
|
|
+ public OkHttpClient okHttpClient(@Qualifier("SSLSocketFactory") SSLSocketFactory sslSocketFactory, @Qualifier("X509TrustManager") X509TrustManager x509TrustManager){
|
|
|
|
|
+ return new OkHttpClient.Builder()
|
|
|
|
|
+ .sslSocketFactory(sslSocketFactory, x509TrustManager)
|
|
|
|
|
+ .hostnameVerifier(new SkipHostnameVerifier())
|
|
|
|
|
+ .retryOnConnectionFailure(true)//当使用连接池时该参数设置true才有意义
|
|
|
|
|
+ .connectionPool(new okhttp3.ConnectionPool(200, 5, TimeUnit.MINUTES))
|
|
|
|
|
+ .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .writeTimeout(30,TimeUnit.SECONDS)
|
|
|
|
|
+ .addNetworkInterceptor(new Interceptor() {//添加网络拦截器
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Response intercept(Chain chain) throws IOException {
|
|
|
|
|
+ Request oldRequest = chain.request();
|
|
|
|
|
+ //在这里可以添加或修改请求的header,比如添加User-Agent
|
|
|
|
|
+ //Request newRequest = oldRequest.newBuilder()
|
|
|
|
|
+ // .header("User-Agent", "Linux")
|
|
|
|
|
+ // .build();
|
|
|
|
|
+ //Response oldResponse = chain.proceed(newRequest);
|
|
|
|
|
+ //重写Response
|
|
|
|
|
+ //return oldResponse.newBuilder()
|
|
|
|
|
+ // .header("Cache-Control", "max-age=60")
|
|
|
|
|
+ // .build();
|
|
|
|
|
+ return chain.proceed(oldRequest);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ //.addInterceptor(new Interceptor() {//添加应用拦截器
|
|
|
|
|
+ // @Override
|
|
|
|
|
+ // public Response intercept(Chain chain) throws IOException {
|
|
|
|
|
+ // return null;
|
|
|
|
|
+ // }
|
|
|
|
|
+ //})
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "OkHttpClientProxy")
|
|
|
|
|
+ public OkHttpClient okHttpClientProxy(@Qualifier("SSLSocketFactory") SSLSocketFactory sslSocketFactory, @Qualifier("X509TrustManager") X509TrustManager x509TrustManager){
|
|
|
|
|
+ if (!StringUtils.isEmpty(strWWWProxyAddress)){
|
|
|
|
|
+ String ipAndPort[] = strWWWProxyAddress.split(":");
|
|
|
|
|
+ if (ipAndPort != null && ipAndPort.length >= 2){
|
|
|
|
|
+ return new OkHttpClient.Builder()
|
|
|
|
|
+ .sslSocketFactory(sslSocketFactory, x509TrustManager)
|
|
|
|
|
+ .hostnameVerifier(new SkipHostnameVerifier())
|
|
|
|
|
+ .retryOnConnectionFailure(true)//当使用连接池时该参数设置true才有意义
|
|
|
|
|
+ .connectionPool(new okhttp3.ConnectionPool(200, 5, TimeUnit.MINUTES))
|
|
|
|
|
+ .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .writeTimeout(30,TimeUnit.SECONDS)
|
|
|
|
|
+ .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipAndPort[0], Integer.valueOf(ipAndPort[1]))))
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return new OkHttpClient.Builder()
|
|
|
|
|
+ .sslSocketFactory(sslSocketFactory, x509TrustManager)
|
|
|
|
|
+ .hostnameVerifier(new SkipHostnameVerifier())
|
|
|
|
|
+ .retryOnConnectionFailure(true)//当使用连接池时该参数设置true才有意义
|
|
|
|
|
+ .connectionPool(new okhttp3.ConnectionPool(200, 5, TimeUnit.MINUTES))
|
|
|
|
|
+ .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .readTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
+ .writeTimeout(30,TimeUnit.SECONDS)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "X509TrustManager")
|
|
|
|
|
+ public X509TrustManager x509TrustManager() {
|
|
|
|
|
+ return new X509TrustManager() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void checkClientTrusted(X509Certificate[] x509Certificates, String s){
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void checkServerTrusted(X509Certificate[] x509Certificates, String s){
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
+ return new X509Certificate[0];
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "SSLSocketFactory")
|
|
|
|
|
+ public SSLSocketFactory sslSocketFactory(@Qualifier("X509TrustManager") X509TrustManager x509TrustManager) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SSLContext context = SSLContext.getInstance("TLS");
|
|
|
|
|
+ context.init(null, new TrustManager[] {x509TrustManager}, new SecureRandom());
|
|
|
|
|
+ return context.getSocketFactory();
|
|
|
|
|
+ } catch (NoSuchAlgorithmException | KeyManagementException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static class SkipHostnameVerifier implements HostnameVerifier {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean verify(String s, SSLSession sslSession) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /*@Bean
|
|
|
|
|
+ public ServiceMgrProxy startServiceMgrProxy(){
|
|
|
|
|
+ GlobalData.getInstance().setSwitchManualHttpGiz(false);
|
|
|
|
|
+ ServiceMgrProxy.getInstance().init();
|
|
|
|
|
+ return ServiceMgrProxy.getInstance();
|
|
|
|
|
+ }*/
|
|
|
|
|
+}
|