栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用Java URL连接完全忽略ssl的简单方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Java URL连接完全忽略ssl的简单方法是什么?

有一个解决方案,在这里它优雅地为我工作。刚打电话

SSLUtilities.trustAllHostnames();SSLUtilities.trustAllHttpsCertificates();

在进行SSL连接之前。

您还可以通过在Internet上搜索来获取更多解决方案

java ssl trustall

这是该解决方案的副本(以防将来链接可能断开):

 import java.security.GeneralSecurityException; import java.security.SecureRandom; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager;  public final class SSLUtilities {      private static com.sun.net.ssl.HostnameVerifier __hostnameVerifier;      private static com.sun.net.ssl.TrustManager[] __trustManagers;      private static HostnameVerifier _hostnameVerifier;      private static TrustManager[] _trustManagers;      private static void __trustAllHostnames() {       // Create a trust manager that does not validate certificate chains       if(__hostnameVerifier == null) {__hostnameVerifier = new _FakeHostnameVerifier();       } // if       // Install the all-trusting host name verifier       com.sun.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(__hostnameVerifier);   } // __trustAllHttpsCertificates      private static void __trustAllHttpsCertificates() {       com.sun.net.ssl.SSLContext context;       // Create a trust manager that does not validate certificate chains       if(__trustManagers == null) {__trustManagers = new com.sun.net.ssl.TrustManager[]     {new _FakeX509TrustManager()};       } // if       // Install the all-trusting trust manager       try {context = com.sun.net.ssl.SSLContext.getInstance("SSL");context.init(null, __trustManagers, new SecureRandom());       } catch(GeneralSecurityException gse) {throw new IllegalStateException(gse.getMessage());       } // catch       com.sun.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());   } // __trustAllHttpsCertificates      private static boolean isDeprecatedSSLProtocol() {       return("com.sun.net.ssl.internal.www.protocol".equals(System.getProperty("java.protocol.handler.pkgs")));   } // isDeprecatedSSLProtocol      private static void _trustAllHostnames() {       // Create a trust manager that does not validate certificate chains       if(_hostnameVerifier == null) {_hostnameVerifier = new FakeHostnameVerifier();       } // if         // Install the all-trusting host name verifier:       HttpsURLConnection.setDefaultHostnameVerifier(_hostnameVerifier);   } // _trustAllHttpsCertificates      private static void _trustAllHttpsCertificates() {       SSLContext context;       // Create a trust manager that does not validate certificate chains       if(_trustManagers == null) {_trustManagers = new TrustManager[] {new FakeX509TrustManager()};       } // if       // Install the all-trusting trust manager:       try {       context = SSLContext.getInstance("SSL");       context.init(null, _trustManagers, new SecureRandom());       } catch(GeneralSecurityException gse) {throw new IllegalStateException(gse.getMessage());       } // catch       HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());   } // _trustAllHttpsCertificates      public static void trustAllHostnames() {       // Is the deprecated protocol setted?       if(isDeprecatedSSLProtocol()) {__trustAllHostnames();       } else {_trustAllHostnames();       } // else   } // trustAllHostnames      public static void trustAllHttpsCertificates() {       // Is the deprecated protocol setted?       if(isDeprecatedSSLProtocol()) {__trustAllHttpsCertificates();       } else {_trustAllHttpsCertificates();       } // else   } // trustAllHttpsCertificates      public static class _FakeHostnameVerifier        implements com.sun.net.ssl.HostnameVerifier {              public boolean verify(String hostname, String session) {return(true);       } // verify   } // _FakeHostnameVerifier      public static class _FakeX509TrustManager        implements com.sun.net.ssl.X509TrustManager {              private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};              public boolean isClientTrusted(X509Certificate[] chain) {return(true);       } // checkClientTrusted              public boolean isServerTrusted(X509Certificate[] chain) {return(true);       } // checkServerTrusted              public X509Certificate[] getAcceptedIssuers() {return(_AcceptedIssuers);       } // getAcceptedIssuers   } // _FakeX509TrustManager      public static class FakeHostnameVerifier implements HostnameVerifier {              public boolean verify(String hostname, javax.net.ssl.SSLSession session) {return(true);       } // verify   } // FakeHostnameVerifier      public static class FakeX509TrustManager implements X509TrustManager {              private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};              public void checkClientTrusted(X509Certificate[] chain, String authType) {       } // checkClientTrusted              public void checkServerTrusted(X509Certificate[] chain, String authType) {       } // checkServerTrusted              public X509Certificate[] getAcceptedIssuers() {return(_AcceptedIssuers);       } // getAcceptedIssuers   } // FakeX509TrustManager } // SSLUtilities


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449600.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号