gpt4 book ai didi

java - JVM/Android 不信任 StartSSL 的根证书

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:49 27 4
gpt4 key购买 nike

我已经使用 Apache 设置了一个托管两个网站的 VPS。两者都有带有 StartSSL 证书的(有效)SSL 配置,我可以从桌面或移动浏览器访问它们。

我正在尝试访问其中一个网站使用 SSL 运行的 API,但我遇到了问题。我首先使用 Apache HttpClient(已弃用),但是 it looks like it can't choose the proper certificate on the server because it doesn't support Server Name Indication并且解决方法是使用 HttpsURLConnection。

所以我目前有这段代码,无耻地从互联网上复制:

String url = "https://mywebsite.ext/api/xxx";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add reuqest header
con.setRequestMethod("POST");

String urlParameters = "blah=foo&bar=xx";

// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
// ...

我得到的是:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

(如果我尝试访问例如 https://google.com,我当然不会得到)

经过谷歌搜索,我发现问题似乎是 JVM(桌面)/android 无法识别 StartSSL 的根证书。我不想手动导入它,因为这段代码的最终目的是在 Android 应用程序中运行。正如我在许多答案中看到的那样,我不想通过允许任何证书来失去对 SSL 的所有兴趣。

有什么见解吗?

谢谢

最佳答案

如果您的 CA 不受您的平台信任,除了手动将 CA 添加到平台的根 CA 信任列表或让您的证书由您的平台信任的 CA 之一签名之外,您别无选择。

关于java - JVM/Android 不信任 StartSSL 的根证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259449/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com