javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(298)

本文整理了Java中javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory()方法的一些代码示例,展示了HttpsURLConnection.setDefaultSSLSocketFactory()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpsURLConnection.setDefaultSSLSocketFactory()方法的具体详情如下:
包路径:javax.net.ssl.HttpsURLConnection
类名称:HttpsURLConnection
方法名:setDefaultSSLSocketFactory

HttpsURLConnection.setDefaultSSLSocketFactory介绍

[英]Sets the default SSL socket factory to be used by new instances.
[中]设置新实例要使用的默认SSL套接字工厂。

代码示例

代码示例来源:origin: stackoverflow.com

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCertificates, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);

代码示例来源:origin: apache/cloudstack

sc.init(null, trustAllCerts, new SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(new SecureSSLSocketFactory(sc));
  HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {

代码示例来源:origin: apache/attic-polygene-java

@BeforeClass
public static void beforeSecureClass()
  throws IOException, GeneralSecurityException
{
  defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
  defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
  HttpsURLConnection.setDefaultHostnameVerifier( ( string, ssls ) -> true );
  HttpsURLConnection.setDefaultSSLSocketFactory( buildTrustSSLContext().getSocketFactory() );
}

代码示例来源:origin: stackoverflow.com

HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new NullX509TrustManager()}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

代码示例来源:origin: stackoverflow.com

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
  public X509Certificate[] getAcceptedIssuers(){return null;}
  public void checkClientTrusted(X509Certificate[] certs, String authType){}
  public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};

// Install the all-trusting trust manager
try {
  SSLContext sc = SSLContext.getInstance("TLS");
  sc.init(null, trustAllCerts, new SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
  ;
}

代码示例来源:origin: stackoverflow.com

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { 
  new X509TrustManager() {     
    public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return new X509Certificate[0];
    } 
    public void checkClientTrusted( 
      java.security.cert.X509Certificate[] certs, String authType) {
      } 
    public void checkServerTrusted( 
      java.security.cert.X509Certificate[] certs, String authType) {
    }
  } 
}; 

// Install the all-trusting trust manager
try {
  SSLContext sc = SSLContext.getInstance("SSL"); 
  sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (GeneralSecurityException e) {
} 
// Now you can access an https URL without having the certificate in the truststore
try { 
  URL url = new URL("https://hostname/index.html"); 
} catch (MalformedURLException e) {
}

代码示例来源:origin: stackoverflow.com

SSLContext sslcontext = SSLContext.getInstance("TLSv1");
     sslcontext.init(null,
         null,
         null);
     SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
     HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);
     l_connection = (HttpsURLConnection) l_url.openConnection();
     l_connection.connect();

代码示例来源:origin: stackoverflow.com

SSLContext sslcontext = SSLContext.getInstance("TLSv1");
     sslcontext.init(null,
         null,
         null);
     SSLSocketFactory NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
     HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);
     l_connection = (HttpsURLConnection) l_url.openConnection();
     l_connection.connect();

代码示例来源:origin: stackoverflow.com

KeyStore clientStore = KeyStore.getInstance("PKCS12");
   clientStore.load(new FileInputStream("test.p12"), "testPass".toCharArray());
   KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
   kmf.init(clientStore, "testPass".toCharArray());
   KeyManager[] kms = kmf.getKeyManagers();
   KeyStore trustStore = KeyStore.getInstance("JKS");
   trustStore.load(new FileInputStream("cacerts"), "changeit".toCharArray());
   TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   tmf.init(trustStore);
   TrustManager[] tms = tmf.getTrustManagers();
   SSLContext sslContext = null;
   sslContext = SSLContext.getInstance("TLS");
   sslContext.init(kms, tms, new SecureRandom());
   HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
   URL url = new URL("https://www.testurl.com");
   HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();

代码示例来源:origin: knowm/XChange

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

代码示例来源:origin: jphp-group/jphp

@Signature
  public static void disableSSLVerificationForHttps() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
          }

          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }

          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }

        }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
      public boolean verify(String hostname, SSLSession session) {
        return true;
      }
    };

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  }
}

代码示例来源:origin: stackoverflow.com

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
  new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
      return null;
    }
    public void checkClientTrusted(
      java.security.cert.X509Certificate[] certs, String authType) {
    }
    public void checkServerTrusted(
      java.security.cert.X509Certificate[] certs, String authType) {
    }
  }
};

// Install the all-trusting trust manager
try {
  SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}

// Now you can access an https URL without having the certificate in the truststore
try {
  URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}

代码示例来源:origin: stackoverflow.com

SSLContext sc = SSLContext.getInstance("SSL");
 sc.init(null, trustAllCerts, new SecureRandom());
 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception e) {}

代码示例来源:origin: stackoverflow.com

private void trustEveryone() { 
  try { 
      HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 
          public boolean verify(String hostname, SSLSession session) { 
              return true; 
          }}); 
      SSLContext context = SSLContext.getInstance("TLS"); 
      context.init(null, new X509TrustManager[]{new X509TrustManager(){ 
          public void checkClientTrusted(X509Certificate[] chain, 
                  String authType) throws CertificateException {} 
          public void checkServerTrusted(X509Certificate[] chain, 
                  String authType) throws CertificateException {} 
          public X509Certificate[] getAcceptedIssuers() { 
              return new X509Certificate[0]; 
          }}}, new SecureRandom()); 
      HttpsURLConnection.setDefaultSSLSocketFactory( 
              context.getSocketFactory()); 
  } catch (Exception e) { // should never happen 
      e.printStackTrace(); 
  } 
}

代码示例来源:origin: stackoverflow.com

SSLContext sc = SSLContext.getInstance("TLS");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection
      .setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
  e.printStackTrace();

代码示例来源:origin: apache/geode

private void configureHttpsURLConnection(SSLConfig sslConfig, boolean skipSslVerification)
  throws Exception {
 KeyManager[] keyManagers = getKeyManagers(sslConfig);
 TrustManager[] trustManagers = getTrustManagers(sslConfig, skipSslVerification);
 if (skipSslVerification) {
  HttpsURLConnection.setDefaultHostnameVerifier((String s, SSLSession sslSession) -> true);
 }
 SSLContext ssl =
   SSLContext.getInstance(SSLUtil.getSSLAlgo(SSLUtil.readArray(sslConfig.getProtocols())));
 ssl.init(keyManagers, trustManagers, new SecureRandom());
 HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());
}

代码示例来源:origin: stackoverflow.com

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
      public boolean verify(String hostname, SSLSession session) {
        return true;
      }});
  SSLContext context = SSLContext.getInstance("TLS");
  context.init(null, new X509TrustManager[]{new X509TrustManager(){
    public void checkClientTrusted(X509Certificate[] chain,
        String authType) throws CertificateException {}
    public X509Certificate[] getAcceptedIssuers() {
      return new X509Certificate[0];
    }}}, new SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(
      context.getSocketFactory());
} catch (Exception e) { // should never happen
  e.printStackTrace();

代码示例来源:origin: knowm/XChange

SSLContext sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] {xtm};
sslContext.init(null, xtmArray, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
HttpsURLConnection httpsUrlConn = (HttpsURLConnection) (new URL(serverUrl)).openConnection();
httpsUrlConn.setRequestMethod("POST");

代码示例来源:origin: knowm/XChange

SSLContext sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] {xtm};
sslContext.init(null, xtmArray, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
HttpsURLConnection httpsUrlConn = (HttpsURLConnection) (new URL(serverUrl)).openConnection();
httpsUrlConn.setRequestMethod("POST");

代码示例来源:origin: stackoverflow.com

SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
  e.printStackTrace();

相关文章

HttpsURLConnection类方法