我目前正在开发一个 Android 应用程序,我需要在该应用程序上执行连接到测试服务器的 SSLSocket。
问题是我们不会为该服务器购买证书,因为它是测试服务器,而不是生产服务器。
虽然它在模拟器上运行良好,但是在我的 S3 上它给了我这个错误:
06-01 00:36:17.355: I/System.out(13629): java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
此代码的最后一行抛出异常:
mSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
mSocket = (SSLSocket) mSf.createSocket("myTestingServer.com", 443);
mSocket.startHandshake();
我真的不在乎解决方案是编码还是降低 Android 用户的安全检查,因为它是在我的 S3 上测试,一旦投入生产,证书应该是好的。 这不是 HTTP 服务器,所以我不能使用 org.apache.http.org.ssl
最佳答案
也许您想忽略证书检查
警告:此代码不安全
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sslsocketfactory = sc.getSocketFactory();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("google.com", 443);
...
来源:Using Java to connect with SSLSocket, trusting all certificates
关于android - SSLSocket 连接信任证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16866526/