99import com .fengwenyi .javalib .util .StrUtils ;
1010import okhttp3 .*;
1111
12+ import javax .net .ssl .*;
1213import java .io .IOException ;
1314import java .rmi .RemoteException ;
15+ import java .security .KeyManagementException ;
16+ import java .security .NoSuchAlgorithmException ;
17+ import java .security .SecureRandom ;
18+ import java .security .cert .X509Certificate ;
1419import java .time .Duration ;
1520import java .util .List ;
1621import 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}
0 commit comments