Skip to content

Commit

Permalink
修复okhttp网络请求没有设置ssl的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fengwenyi committed Aug 17, 2024
1 parent 4dce3d9 commit 5ec1f1f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
import com.fengwenyi.javalib.util.StrUtils;
import okhttp3.*;

import javax.net.ssl.*;
import java.io.IOException;
import java.rmi.RemoteException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.List;
import java.util.Map;
Expand All @@ -36,6 +41,8 @@ public Response execute(Request request, Request.Option option) throws IOExcepti

private okhttp3.OkHttpClient client(Request.Option option) {
okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder();
HostnameVerifier hostnameVerifier = null;
SSLSocketFactory sslContextFactory = null;
if (Objects.nonNull(option)) {
Integer connectTimeoutSecond = getTimeoutSecond(option.getConnectTimeoutSecond());
if (Objects.nonNull(connectTimeoutSecond)) {
Expand All @@ -45,7 +52,17 @@ private okhttp3.OkHttpClient client(Request.Option option) {
if (Objects.nonNull(readTimeoutSecond)) {
builder.readTimeout(Duration.ofSeconds(readTimeoutSecond));
}
hostnameVerifier = option.getHostnameVerifier();
sslContextFactory = option.getSslContextFactory();
}
if (Objects.isNull(hostnameVerifier)) {
hostnameVerifier = getIgnoreSslHostnameVerifier();
}
if (Objects.isNull(sslContextFactory)) {
sslContextFactory = getIgnoreInitedSslContext().getSocketFactory();
}
builder.sslSocketFactory(sslContextFactory, IGNORE_SSL_TRUST_MANAGER_X509);
builder.hostnameVerifier(hostnameVerifier);
return builder.build();
}

Expand Down Expand Up @@ -194,7 +211,6 @@ private Response upload(Request request, Request.Option option) {
// 创建 MediaType 对象
MediaType mediaType = MediaType.parse("multipart/form-data; charset=utf-8");


MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM);

Expand Down Expand Up @@ -226,4 +242,67 @@ private Map<String, String> getHeaderMap(Request.Option option) {
return option.getHeaders();
}

/**
* Get initialized SSLContext instance which ignored SSL certification
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static SSLContext getIgnoreInitedSslContext() {
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
try {
sslContext.init(null, trustAllCerts, new SecureRandom());
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
return sslContext;
}

private static final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};

/**
* Get HostnameVerifier which ignored SSL certification
*
* @return
*/
public static HostnameVerifier getIgnoreSslHostnameVerifier() {
return (hostname, sslSession ) -> true;
}

public static final X509TrustManager IGNORE_SSL_TRUST_MANAGER_X509 = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
};

}
7 changes: 7 additions & 0 deletions src/test/java/com/fengwenyi/javalib/http/HttpUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ public void testFull() {
}
}

@Test
public void testGetXzqh() {
String url = "https://www.mca.gov.cn/mzsj/xzqh/2023/202301xzqh.html";
String result = HttpUtils.get(url);
System.out.println(result);
}

}

0 comments on commit 5ec1f1f

Please sign in to comment.