content: package my.project.moviesbox.config;
  
  import java.security.cert.CertificateException;
  import java.util.Arrays;
  import java.util.concurrent.TimeUnit;
  
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSocketFactory;
  import javax.net.ssl.TrustManager;
  import javax.net.ssl.X509TrustManager;
  
  import okhttp3.ConnectionSpec;
  import okhttp3.OkHttpClient;
  import okhttp3.TlsVersion;
  
  /**
   * @author Li
   * @version 1.0
   * @description: 注释
   * @date 2024/1/29 8:56
   */
  public class GlideUnsafeOkHttpClient {
      public static OkHttpClient getUnsafeOkHttpClient() {
          try {
              // Create a trust manager that does not validate certificate chains
              final TrustManager[] trustAllCerts = new TrustManager[]{
                      new X509TrustManager() {
                          @Override
                          public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                                  throws CertificateException {
                          }
  
                          @Override
                          public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                                  throws CertificateException {
                          }
  
                          @Override
                          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                              return new java.security.cert.X509Certificate[]{};
                          }
                      }
              };
  
              // Install the all-trusting trust manager
              final SSLContext sslContext = SSLContext.getInstance("SSL");
              sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
  
              // Create an ssl socket factory with our all-trusting manager
              final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
  
              OkHttpClient.Builder builder = new OkHttpClient.Builder();
              builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
              // 兼容一些老服务器（TLS 1.0/1.1/1.2）
              builder.connectionSpecs(Arrays.asList(new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                              .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
                              .allEnabledCipherSuites()
                              .build(),
                      ConnectionSpec.CLEARTEXT)); // 支持明文 HTTP
              builder.hostnameVerifier((hostname, session) -> true);
  
              builder.connectTimeout(20, TimeUnit.SECONDS);
              builder.readTimeout(20, TimeUnit.SECONDS);
  
              OkHttpClient okHttpClient = builder.build();
              return okHttpClient;
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  }
  