我正在尝试连接到运行 godaddy 256 位 SSL 证书的 IIS6 机器,但出现错误:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
一直在尝试确定可能导致这种情况的原因,但现在一片空白。
这是我的连接方式:
HttpsURLConnection conn;
conn = (HttpsURLConnection) (new URL(mURL)).openConnection();
conn.setConnectTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
String tempString = toString(conn.getInputStream());
最佳答案
与已接受的答案相反,您不需要需要自定义信任管理器,您需要修复服务器配置!
我在使用错误安装的 dynadot/alphassl 证书连接到 Apache 服务器时遇到了同样的问题。我正在使用抛出的 HttpsUrlConnection (Java/Android) 进行连接 -
javax.net.ssl.SSLHandshakeException:
java.security.cert.CertPathValidatorException:
Trust anchor for certification path not found.
实际问题是服务器配置错误 - 用 http://www.digicert.com/help/ 测试它或类似的,它甚至会告诉你解决方案:
“该证书未由可信机构签署(对照 Mozilla 的根存储进行检查)。如果您从可信机构购买证书,您可能只需要安装一个或多个中间证书。请联系您的证书提供商以获得针对您的服务器平台执行此操作的帮助。”
也可以用openssl查看证书:
openssl s_client -debug -connect www.thedomaintocheck.com:443
您可能会看到:
验证返回码:21(无法验证第一个证书)
并且,在输出的前面:
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=27:certificate not trusted
verify return:1
depth=0 OU = Domain Control Validated, CN = www.thedomaintocheck.com
verify error:num=21:unable to verify the first certificate`
证书链将只包含 1 个元素(您的证书):
Certificate chain
0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
i:/O=AlphaSSL/CN=AlphaSSL CA - G2
...但应该将链中的签名机构引用回 Android 信任的机构(Verisign、GlobalSign 等):
Certificate chain
0 s:/OU=Domain Control Validated/CN=www.thedomaintocheck.com
i:/O=AlphaSSL/CN=AlphaSSL CA - G2
1 s:/O=AlphaSSL/CN=AlphaSSL CA - G2
i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
2 s:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
i:/C=BE/O=GlobalSign nv-sa/OU=Root CA/CN=GlobalSign Root CA
配置服务器的说明(和中间证书)通常由颁发证书的机构提供,例如:http://www.alphassl.com/support/install-root-certificate.html
安装我的证书颁发者提供的中间证书后,我现在在使用 HttpsUrlConnection 连接时没有错误。
关于android - 未找到 Android SSL 连接的信任 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222777/