首页 > 解决方案 > android studio:https自签名证书不受信任

问题描述

尝试通过 android studio(使用 kotlin)访问具有我自己的自签名证书的https://localhost:5000/,但我不断收到错误消息:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。

我通过使用方法 2在此处找到的指南使用 openssl 制作了证书

我尝试添加一个如下所示的 network_security_config.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">https://10.0.2.2:5000/</domain>
        <trust-anchors>
            <certificates src="@raw/my_ca"/>
            <certificates src="user"/>
            <certificates src="system"/>
        </trust-anchors>
    </domain-config>
</network-security-config>

我在 res 中创建了一个名为 raw 的文件夹,并将其放入 my_ca.pem,(我使用从 crt 转换openssl x509 -in server.crt -out server.pem -outform PEM并重命名为 my_ca.pem)

我还尝试在 android studio 设置中添加该证书,或者允许所有不受信任的站点,html 请求的代码就像我发现的一样简单:

URL("https://10.0.2.2:5000/").readText()

,它在 AsyncTask 类中,但该类所做的只是这个请求

我访问的是 10.0.2.2 而不是 127.0.0.1 因为据我了解,它是通过 te 模拟器访问本地机器的方式,并且 erver 代码是一个非常简单的 python-flask 代码,它返回一个简单的字符串

标签: android-studiosslopensslca

解决方案


所以我想通了,虽然我确实在 android studio 上启用了自签名证书,但我没有在 kotlin 上启用它们,所以错误来自 kotlin 而不是 android studio,从这个答案快速复制粘贴

已经解决了它,尽管我对此一无所知,如果有人可以给我发一份关于某种解释或教程的参考,那就太好了。


推荐阅读