有一个解决方案,在这里它优雅地为我工作。刚打电话
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)