소스 검색

增加OkHttp配置文件读取

tags/v1.0.0^2
林万龙 3 년 전
부모
커밋
a89f386841
2개의 변경된 파일100개의 추가작업 그리고 1개의 파일을 삭제
  1. +1
    -1
      src/main/java/com/telpo/dipperposition/common/OkHttpUtil.java
  2. +99
    -0
      src/main/java/com/telpo/dipperposition/config/OkHttpConfig.java

+ 1
- 1
src/main/java/com/telpo/dipperposition/common/OkHttpUtil.java 파일 보기

@@ -23,7 +23,7 @@ import java.util.Objects;
@Component
public class OkHttpUtil {

private OkHttpClient okHttpClient = new OkHttpClient();
@Autowired private OkHttpClient okHttpClient;

/**
* 根据map获取get请求参数


+ 99
- 0
src/main/java/com/telpo/dipperposition/config/OkHttpConfig.java 파일 보기

@@ -0,0 +1,99 @@
package com.telpo.dipperposition.config;

import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

/**
* @program: DataPushServer
* @description: http设置
* @author: linwl
* @create: 2020-07-17 15:29
*/
@Configuration
@Slf4j
public class OkHttpConfig {

@Value("${OkHttp-config.pool.max-conn}")
private Integer maxConn;

@Value("${OkHttp-config.pool.keep-alive}")
private Integer keepAlive;

@Value("${OkHttp-config.read-timeout}")
private long readTimeout;

@Value("${OkHttp-config.conn-timeout}")
private long connTimeout;

@Value("${OkHttp-config.write-timeout}")
private long writeTimeout;

@Bean
public X509TrustManager x509TrustManager() {
return new X509TrustManager() {
@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];
}
};
}

@Bean
public SSLSocketFactory sslSocketFactory() {
try {
// 信任任何链接
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] {x509TrustManager()}, new SecureRandom());
return sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
log.error("okhttp3 send https error:", e);
}
return null;
}

/**
* Create a new connection pool with tuning parameters appropriate for a single-user application.
* The tuning parameters in this pool are subject to change in future OkHttp releases. Currently
*/
@Bean
public ConnectionPool pool() {
return new ConnectionPool(maxConn, keepAlive, TimeUnit.MINUTES);
}

@Bean
public OkHttpClient okHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder
.connectTimeout(connTimeout, TimeUnit.SECONDS)
.readTimeout(readTimeout, TimeUnit.SECONDS)
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
.connectionPool(pool()) // 连接池
.retryOnConnectionFailure(true)
.hostnameVerifier((s, sslSession) -> true)
.sslSocketFactory(sslSocketFactory(), x509TrustManager());
return builder.build();
}
}

Loading…
취소
저장