Android KSOAP2 SSL java.security.cert.CertPathValidatorException

Android KSOAP2 SSL java.security.cert.CertPathValidatorException,第1张

概述我尝试通过SSL连接到我的JAX-WS服务.没有SSL一切正常.AsyncTask中的方法:HttpsTransportSEandroidHttpTransport=newHttpsTransportSE("10.0.2.2",8181,"/Server/?wsdl",10000);((HttpsServiceConnectionSE)androidHttpTransport.getServiceConn

我尝试通过SSL连接到我的JAX-WS服务.没有SSL一切正常.

AsyncTask中的方法:

     httpsTransportSE  androIDhttpTransport = new httpsTransportSE("10.0.2.2", 8181, "/Server/?wsdl", 10000);             ((httpsServiceConnectionSE) androIDhttpTransport.getServiceConnection()).setSSLSocketFactory(trustAllHosts().getSocketFactory());             //androIDhttpTransport.deBUG=true;             androIDhttpTransport.call(getSoapAction(method), envelope);

获取SSLContext

public SSLContext allowAllSSL() {            SSLContext context = null;             TrustManager[] trustManagers = null;            try{            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());             KeyStore keyStore = KeyStore.getInstance("pkcs12");             inputStream in = cntx.getResources().openRawResource(R.raw.clIEnt_keystore);             try {             keyStore.load(in, "password".tochararray());             } catch (CertificateException e) {             // Todo auto-generated catch block             e.printstacktrace();             } finally {             in.close();             }             tmf.init(keyStore);                if (trustManagers == null) {                         trustManagers = new TrustManager[] { new Fakex509trustmanager() };                 }                 try {                         context = SSLContext.getInstance("SSL");                         context.init(null, tmf.getTrustManagers(), new SecureRandom());                 } catch (NoSuchAlgorithmException e) {                         e.printstacktrace();                 } catch (KeyManagementException e) {                         e.printstacktrace();                 }            httpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());           httpsURLConnection.setDefaultHostnameVerifIEr(new HostnameVerifIEr() {               public boolean verify(String hostname, SSLSession session) {                    return true;                  }                });            }catch(Exception ex)            {                Log.e(TAG,"allowAllSSL Failed: "+ex.toString());            }           return context;        } 

我收到此错误日志:

12-18 07:51:42.161: E/Example:logonAsync(3161): doInBackground Failed: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValIDatorException: Trust anchor for certification path not found.12-18 07:51:42.161: W/System.err(3161): javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValIDatorException: Trust anchor for certification path not found.12-18 07:51:42.169: W/System.err(3161):     at org.apache.harmony.xnet.provIDer.Jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:401)12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.httpconnection.setupSecureSocket(httpconnection.java:209)12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.httpsURLConnectionImpl$httpsEngine.makeSslConnection(httpsURLConnectionImpl.java:478)12-18 07:51:42.169: W/System.err(3161):     at libcore.net.http.httpsURLConnectionImpl$httpsEngine.connect(httpsURLConnectionImpl.java:433)

解决方法:

我发现问我的问题:在MainAsync中:

 httpsTransportSE  androIDhttpTransport = new httpsTransportSE(10.0.2.2, 8181, "/server/?wsdl", 10000);             ((httpsServiceConnectionSE) androIDhttpTransport.getServiceConnection()).setSSLSocketFactory(trustAllHosts().getSocketFactory());protected  SSLContext trustAllHosts(){       return allowAllSSL();} public SSLContext allowAllSSL() {        SSLContext context = null;         TrustManager[] trustManagers = null;        KeyManagerFactory mgrFact;        try{        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());        mgrFact = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());         KeyStore keyStore = KeyStore.getInstance("pkcs12");         inputStream in = cntx.getResources().openRawResource(R.raw.keystore);         try {         keyStore.load(in, "password".tochararray());         mgrFact.init(keyStore, "password".tochararray());         } catch (CertificateException e) {         // Todo auto-generated catch block         e.printstacktrace();         } finally {         in.close();         }         tmf.init(keyStore);            httpsURLConnection.setDefaultHostnameVerifIEr(new HostnameVerifIEr()             {                        @OverrIDe                     public boolean verify(String hostname, SSLSession session) {                             return true;                     }             });             if (trustManagers == null) {                     trustManagers = new TrustManager[] { new Fakex509trustmanager() };             }            final TrustManager[] trustAllCerts = new TrustManager[] {new x509trustmanager() {                public X509Certificate[] getAcceptedissuers() {                    System.out.println("getAcceptedissuers");                     return null;                }                public voID checkServerTrusted(X509Certificate[] chain, String authType)                        throws CertificateException {                    System.out.println("Сведения о сертификате : " +       chain[0].getIssuerX500Principal().getname() + "\n Тип авторизации : " + authType);                }                public voID checkClIEntTrusted(X509Certificate[] chain, String authType)                        throws CertificateException {                    System.out.println("checkClIEntTrusted : " + authType);                }            } };            //tmf.getTrustManagers()            try {                     context = SSLContext.getInstance("TLS");                     context.init(mgrFact.getKeyManagers(), trustAllCerts, new SecureRandom());             } catch (NoSuchAlgorithmException e) {                     e.printstacktrace();             } catch (KeyManagementException e) {                     e.printstacktrace();             }        httpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());       httpsURLConnection.setDefaultHostnameVerifIEr(new HostnameVerifIEr() {           public boolean verify(String hostname, SSLSession session) {                return true;              }            });        }catch(Exception ex)        {            Log.e(TAG,"allowAllSSL Failed: "+ex.toString());        }       return context;    } 
总结

以上是内存溢出为你收集整理的Android KSOAP2 SSL java.security.cert.CertPathValidatorException全部内容,希望文章能够帮你解决Android KSOAP2 SSL java.security.cert.CertPathValidatorException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098390.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存