Skip to content

Commit 5ec1f1f

Browse files
committed
修复okhttp网络请求没有设置ssl的问题
1 parent 4dce3d9 commit 5ec1f1f

2 files changed

Lines changed: 87 additions & 1 deletion

File tree

src/main/java/com/fengwenyi/javalib/http/client/impl/OkHttpClient.java

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
import com.fengwenyi.javalib.util.StrUtils;
1010
import okhttp3.*;
1111

12+
import javax.net.ssl.*;
1213
import java.io.IOException;
1314
import java.rmi.RemoteException;
15+
import java.security.KeyManagementException;
16+
import java.security.NoSuchAlgorithmException;
17+
import java.security.SecureRandom;
18+
import java.security.cert.X509Certificate;
1419
import java.time.Duration;
1520
import java.util.List;
1621
import java.util.Map;
@@ -36,6 +41,8 @@ public Response execute(Request request, Request.Option option) throws IOExcepti
3641

3742
private okhttp3.OkHttpClient client(Request.Option option) {
3843
okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder();
44+
HostnameVerifier hostnameVerifier = null;
45+
SSLSocketFactory sslContextFactory = null;
3946
if (Objects.nonNull(option)) {
4047
Integer connectTimeoutSecond = getTimeoutSecond(option.getConnectTimeoutSecond());
4148
if (Objects.nonNull(connectTimeoutSecond)) {
@@ -45,7 +52,17 @@ private okhttp3.OkHttpClient client(Request.Option option) {
4552
if (Objects.nonNull(readTimeoutSecond)) {
4653
builder.readTimeout(Duration.ofSeconds(readTimeoutSecond));
4754
}
55+
hostnameVerifier = option.getHostnameVerifier();
56+
sslContextFactory = option.getSslContextFactory();
4857
}
58+
if (Objects.isNull(hostnameVerifier)) {
59+
hostnameVerifier = getIgnoreSslHostnameVerifier();
60+
}
61+
if (Objects.isNull(sslContextFactory)) {
62+
sslContextFactory = getIgnoreInitedSslContext().getSocketFactory();
63+
}
64+
builder.sslSocketFactory(sslContextFactory, IGNORE_SSL_TRUST_MANAGER_X509);
65+
builder.hostnameVerifier(hostnameVerifier);
4966
return builder.build();
5067
}
5168

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

197-
198214
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
199215
bodyBuilder.setType(MultipartBody.FORM);
200216

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

245+
/**
246+
* Get initialized SSLContext instance which ignored SSL certification
247+
*
248+
* @return
249+
* @throws NoSuchAlgorithmException
250+
* @throws KeyManagementException
251+
*/
252+
public static SSLContext getIgnoreInitedSslContext() {
253+
SSLContext sslContext = null;
254+
try {
255+
sslContext = SSLContext.getInstance("SSL");
256+
} catch (NoSuchAlgorithmException e) {
257+
throw new RuntimeException(e);
258+
}
259+
try {
260+
sslContext.init(null, trustAllCerts, new SecureRandom());
261+
} catch (KeyManagementException e) {
262+
throw new RuntimeException(e);
263+
}
264+
return sslContext;
265+
}
266+
267+
private static final TrustManager[] trustAllCerts = new TrustManager[] {
268+
new X509TrustManager() {
269+
@Override
270+
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
271+
}
272+
273+
@Override
274+
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
275+
}
276+
277+
@Override
278+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
279+
return new java.security.cert.X509Certificate[]{};
280+
}
281+
}
282+
};
283+
284+
/**
285+
* Get HostnameVerifier which ignored SSL certification
286+
*
287+
* @return
288+
*/
289+
public static HostnameVerifier getIgnoreSslHostnameVerifier() {
290+
return (hostname, sslSession ) -> true;
291+
}
292+
293+
public static final X509TrustManager IGNORE_SSL_TRUST_MANAGER_X509 = new X509TrustManager() {
294+
@Override
295+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
296+
}
297+
298+
@Override
299+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
300+
}
301+
302+
@Override
303+
public X509Certificate[] getAcceptedIssuers() {
304+
return new X509Certificate[] {};
305+
}
306+
};
307+
229308
}

src/test/java/com/fengwenyi/javalib/http/HttpUtilsTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public void testFull() {
7171
}
7272
}
7373

74+
@Test
75+
public void testGetXzqh() {
76+
String url = "https://www.mca.gov.cn/mzsj/xzqh/2023/202301xzqh.html";
77+
String result = HttpUtils.get(url);
78+
System.out.println(result);
79+
}
80+
7481
}

0 commit comments

Comments
 (0)