本文介绍了Android的KSOAP2 SSL java.security.cert.CertPathValidatorException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图连接到我的JAX-WS服务通过SSL。如果没有SSL的所有作品。
在AsyncTask的方法:
HttpsTransportSE androidHttpTransport =新HttpsTransportSE(10.0.2.2,8181,/服务器/ WSDL?,10000);
((HttpsServiceConnectionSE)androidHttpTransport.getServiceConnection())。setSSLSocketFactory(trustAllHosts()
.getSocketFactory());
//androidHttpTransport.debug=true;
androidHttpTransport.call(getSoapAction(方法),信封);
获取的SSL连接
公开的SSL连接allowAllSSL(){
的SSL连接上下文= NULL;
的TrustManager [] trustManagers = NULL;
尝试{
的TrustManagerFactory TMF = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
密钥库的keyStore = KeyStore.getInstance(PKCS12);
InputStream的时间= cntx.getResources()openRawResource(R.raw.client_keystore)。
尝试 {
keyStore.load(在密码.toCharArray());
}赶上(CertificateException E){
// TODO自动生成的catch块
e.printStackTrace();
} 最后 {
附寄();
}
tmf.init(的keyStore);
如果(trustManagers == NULL){
trustManagers =新的TrustManager [] {新FakeX509TrustManager()};
}
尝试 {
上下文= SSLContext.getInstance(SSL);
context.init(空,tmf.getTrustManagers(),新的SecureRandom());
}赶上(抛出:NoSuchAlgorithmException E){
e.printStackTrace();
}赶上(KeyManagementException E){
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
公共布尔验证(字符串主机名,的SSLSession会话){
返回true;
}
});
}赶上(例外前)
{
Log.e(TAG,allowAllSSL失败:+ ex.toString());
}
返回范围内;
}
我得到这个错误日志:
7月一十二号日至18日:51:42.161:E /例:LogOnAsync(3161):doInBackground失败:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:信托没有找到锚认证路径。
7月12号至十八日:51:42.161:W / System.err的(3161):javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径信任锚。
7月12号至十八日:51:42.169:W / System.err的(3161):在org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:401)
7月12号至十八日:51:42.169:W / System.err的(3161):在libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209)
7月12号至十八日:51:42.169:W / System.err的(3161):在libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
7月12号至十八日:51:42.169:W / System.err的(3161):在libcore.net.http.HttpsURLConnectionImpl $ HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
解决方案
我发现问关于我的问题:在MainAsync:
HttpsTransportSE androidHttpTransport =新HttpsTransportSE(10.0.2.2,8181,?/服务器/ WSDL,10000);
((HttpsServiceConnectionSE) androidHttpTransport.getServiceConnection()).setSSLSocketFactory(trustAllHosts().getSocketFactory());
受保护的SSLContext trustAllHosts()
{
返回allowAllSSL();
}
公众的SSLContext allowAllSSL(){
的SSL连接上下文= NULL;
的TrustManager [] trustManagers = NULL;
的KeyManagerFactory mgrFact;
尝试{
的TrustManagerFactory TMF = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
mgrFact = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
密钥库的keyStore = KeyStore.getInstance(PKCS12);
InputStream的时间= cntx.getResources()openRawResource(R.raw.keystore)。
尝试 {
keyStore.load(在密码.toCharArray());
mgrFact.init(密钥库密码.toCharArray());
}赶上(CertificateException E){
// TODO自动生成的catch块
e.printStackTrace();
} 最后 {
附寄();
}
tmf.init(的keyStore);
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier()
{
@覆盖
公共布尔验证(字符串主机名,的SSLSession会话){
返回true;
}
});
如果(trustManagers == NULL){
trustManagers =新的TrustManager [] {新FakeX509TrustManager()};
}
最后的TrustManager [] trustAllCerts =新的TrustManager [] {新X509TrustManager(){
公共x509证书[] getAcceptedIssuers(){
的System.out.println(getAcceptedIssuers);
返回null;
}
公共无效checkServerTrusted(x509证书[]链,字符串的authType)
抛出CertificateException {
的System.out.println(Сведенияосертификате:+连锁[0] .getIssuerX500Principal()的getName()+\ñТипавторизации:+的authType);
}
公共无效checkClientTrusted(x509证书[]链,字符串的authType)
抛出CertificateException {
的System.out.println(checkClientTrusted:+的authType);
}
}};
//tmf.getTrustManagers()
尝试 {
上下文= SSLContext.getInstance(TLS);
context.init(mgrFact.getKeyManagers(),trustAllCerts,新的SecureRandom());
}赶上(抛出:NoSuchAlgorithmException E){
e.printStackTrace();
}赶上(KeyManagementException E){
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
公共布尔验证(字符串主机名,的SSLSession会话){
返回true;
}
});
}赶上(例外前)
{
Log.e(TAG,allowAllSSL失败:+ ex.toString());
}
返回范围内;
}
I tried connect to my JAX-WS service over SSL. Without SSL all works.
Method in 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);
Get 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;
}
I get this error log:
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)
解决方案
I'm found ask on my question: In 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!