Immer SSLHandShakeException
Während ich den Zugriff auf die wsdl-url an, ich bin immer die Ausnahme im android -
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
habe ich verwendet ksoap2 Bibliothek für die Ausführung der WSDL
Datei.
Hatte ich auch umgesetzt-Zertifizierung-Klassen, aber ich bin immer das gleiche Problem
bitte sagen, ob es irgendeine Lösung für dieses.
Ich bin mit diesen beiden Klassen für die Zertifizierung:
AndroidInsecureHttpsServiceConnectionse Klasse:
package com.example.androidwsdltest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import org.ksoap2.HeaderProperty;
import org.ksoap2.transport.ServiceConnection;
import android.util.Log;
public class AndroidInsecureHttpsServiceConnectionSE implements
ServiceConnection {
private HttpsURLConnection connection;
public AndroidInsecureHttpsServiceConnectionSE(String host, int port,
String file, int timeout) throws IOException {
//allowAllSSL();
connection = (HttpsURLConnection) new URL("https", host, port, file)
.openConnection();
updateConnectionParameters(timeout);
}
private static TrustManager[] trustManagers;
public static class EasyX509TrustManager implements X509TrustManager {
private X509TrustManager standardTrustManager = null;
/**
* Constructor for EasyX509TrustManager.
*/
public EasyX509TrustManager(KeyStore keystore)
throws NoSuchAlgorithmException, KeyStoreException {
super();
TrustManagerFactory factory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(keystore);
TrustManager[] trustmanagers = factory.getTrustManagers();
if (trustmanagers.length == 0) {
throw new NoSuchAlgorithmException("no trust manager found");
}
this.standardTrustManager = (X509TrustManager) trustmanagers[0];
}
/**
* @see
* javax.net.ssl.X509TrustManager#checkClientTrusted(X509Certificate
* [],String authType)
*/
public void checkClientTrusted(X509Certificate[] certificates,
String authType) throws CertificateException {
standardTrustManager.checkClientTrusted(certificates, authType);
}
/**
* @see
* javax.net.ssl.X509TrustManager#checkServerTrusted(X509Certificate
* [],String authType)
*/
public void checkServerTrusted(X509Certificate[] certificates,
String authType) throws CertificateException {
if ((certificates != null) && (certificates.length == 1)) {
certificates[0].checkValidity();
} else {
standardTrustManager.checkServerTrusted(certificates, authType);
}
}
/**
* @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
*/
public X509Certificate[] getAcceptedIssuers() {
return this.standardTrustManager.getAcceptedIssuers();
}
}
public static class FakeX509TrustManager implements X509TrustManager {
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
}
public boolean isClientTrusted(X509Certificate[] chain) {
return true;
}
public boolean isServerTrusted(X509Certificate[] chain) {
return true;
}
public X509Certificate[] getAcceptedIssuers() {
return (_AcceptedIssuers);
}
}
/**
* Allow all SSL certificates by setting up a host name verifier that passes
* everything and as well setting up a SocketFactory with the
* #FakeX509TrustManager.
*/
public static void allowAllSSL() {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
//TODO Auto-generated method stub
return true;
}
});
SSLContext context = null;
if (trustManagers == null) {
try {
trustManagers = new TrustManager[] { new EasyX509TrustManager(
null) };
} catch (NoSuchAlgorithmException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
Log.e("allowAllSSL", e.toString());
} catch (KeyManagementException e) {
Log.e("allowAllSSL", e.toString());
}
//HttpsURLConnection.setDefaultAllowUserInteraction(true);
HttpsURLConnection.setDefaultSSLSocketFactory(context
.getSocketFactory());
}
/**
* update the connection with the timeout parameter as well as allowing SSL
* if the Android version is 7 or lower (since these versions have a broken
* certificate manager, which throws a SSL exception saying "Not trusted
* security certificate"
*
* @param timeout
*/
private void updateConnectionParameters(int timeout) {
connection.setConnectTimeout(timeout); //20 seconds
connection.setReadTimeout(timeout); //even if we connect fine we want
//to time out if we cant read
//anything..
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
allowAllSSL();
/*
* int buildVersion = Build.VERSION.SDK_INT; if (buildVersion <= 7) {
* Log.d("Detected old operating system version " + buildVersion +
* " with SSL certificate problems. Allowing " + "all certificates.",
* String.valueOf(buildVersion)); allowAllSSL(); } else {
* Log.d("Full SSL active on new operating system version ",
* String.valueOf(buildVersion)); }
*/
}
public void connect() throws IOException {
connection.connect();
}
public void disconnect() {
connection.disconnect();
}
public List getResponseProperties() {
Map properties = connection.getHeaderFields();
Set keys = properties.keySet();
List retList = new LinkedList();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
List values = (List) properties.get(key);
for (int j = 0; j < values.size(); j++) {
retList.add(new HeaderProperty(key, (String) values.get(j)));
}
}
return retList;
}
public void setRequestProperty(String key, String value) {
//We want to ignore any setting of "Connection: close" because
//it is buggy with Android SSL.
if ("Connection".equalsIgnoreCase(key)
&& "close".equalsIgnoreCase(value)) {
//do nothing
} else {
connection.setRequestProperty(key, value);
}
}
public void setRequestMethod(String requestMethod) throws IOException {
connection.setRequestMethod(requestMethod);
}
public OutputStream openOutputStream() throws IOException {
return connection.getOutputStream();
}
public InputStream openInputStream() throws IOException {
return connection.getInputStream();
}
public InputStream getErrorStream() {
return connection.getErrorStream();
}
public String getHost() {
return connection.getURL().getHost();
}
public int getPort() {
return connection.getURL().getPort();
}
public String getPath() {
return connection.getURL().getPath();
}
}
AndroidInsecureKeepAliveHttpsTransportse Klasse:
package com.example.androidwsdltest;
import java.io.IOException;
import org.ksoap2.transport.HttpsTransportSE;
import org.ksoap2.transport.ServiceConnection;
public class AndroidInsecureKeepAliveHttpsTransportSE extends HttpsTransportSE {
private AndroidInsecureHttpsServiceConnectionSE conn = null;
private final String host;
private final int port;
private final String file;
private final int timeout;
public AndroidInsecureKeepAliveHttpsTransportSE(String host, int port,
String file, int timeout) {
super(host, port, file, timeout);
this.host = host;
this.port = port;
this.file = file;
this.timeout = timeout;
}
@Override
protected ServiceConnection getServiceConnection() throws IOException {
super.getServiceConnection();
conn = new AndroidInsecureHttpsServiceConnectionSE(host, port, file,
timeout);
conn.setRequestProperty("Connection", "keep-alive");
return conn;
}
}
Nach der Verwendung dieser Zertifizierung-Klassen, die ich bin immer die gleiche Ausnahme
Bitte helft mir..
Vielen Dank im Voraus......:)
- Verwenden Sie selbst signierte Zertifikate? Pflege zu zeigen, den code?
- Verbinden Sie sich über vpn?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Müssen Sie von einem autorisierten SSL-Zertifikat - Das könnte das problem lösen, und das ist der beste Weg, das problem zu lösen.
Wenn nicht, musst du ein bisschen arbeiten:
Konzept ist, dass bei der Verwendung von SSL, Android ist wenig härter als üblich, und Sie haben die Zertifikat-Rechte auf Ihrem android, also für das, dass Sie haben eine grundlegende Schlüsselspeicher auf Ihrem Android (wie google store, ist aber kostenlos, und auf Ihre eigene Android), und stellen Sie Ihre eigene vertraut, indem Sie einige grundlegende Manipulationen.
Suchen Sie nach 'Vertrauenswürdigen Stammzertifizierungsstellen'. Finden Sie Ihr Zertifikat aus und exportieren (das keytool laden können vorhandene pfx Zertifikat. auf *.cer-Art des Zertifikats, gibt es dort wenig Probleme.
Sie können installieren, aber ich bevorzuge die folgende Empfehlung:
Dies geschieht durch das Help - Software Updates - Find and install - Suche für die neue Funktion....
Klicken Sie auf New Remote Site " und fügen Sie http://keytool.sourceforge.net/update in den Dateinamen und die URL, und stellen Sie sicher, Ihre geprüft.
Treffer Beenden.
http://developer.android.com/training/articles/security-ssl.html#UnknownCa
Verwenden Sie HttpTransportSE::ServiceConnection::setSSLSocketFactory, wie die Probe.
https://code.google.com/p/androidzon/source/browse/Androidzon/src/it/marco/ksoap2/HttpTransportSE.java?r=77
(Erstellen Sie einfach die neue Funktion der eigenen, und schließen Sie es an den web-service. Wenn nicht funktioniert, entfernen ?wsdl)
Glück!!!
Wird der Fehler durch ein Problem beim Aufbau von Vertrauen mit dem server. Meistens truststore nicht die Zertifikate, die der server übermittelt. Antworten auf folgenden 2 Fragen kann Ihnen dabei helfen.
[1] - Wie Kann ich den Zugriff auf eine SSL-Verbindung Über Android?
[2] - Zertifikat vertrauenswürdig ist, indem Sie dem PC, aber nicht von Android