使用来自 AWS 服务器的自签名证书在 Android 上设置 HTTPS 连接
Setting up an HTTPS connection on Android with a self signed certificate from an AWS server
我在 Android 应用程序的团队中工作,被分配了设置用户登录和注册页面的任务。使用 HttpsURLConnection
class 将我的服务器 class 设置为 post 后,我收到了 SSLHandshakeException
错误。
HttpsURL连接码
HttpsURLConnection connection = (HttpsURLConnection) new URL(spec.toString()).openConnection();
经过一些研究,我意识到这是因为 Android 不信任我们的服务器,因为它是自签名的。我正在尝试从 Android 开发站点关注此 documentation,但不明白如何获取 crt
文件。我使用的是 AWS 服务器,但我不是设置自签名证书的人,但我拥有对服务器的完全访问权限。
来自 Android 文档(如何获取 load-der.crt 文件?)
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
这是我获得证书的方式,虽然这个答案可能对其他人帮助不大。我向服务器管理员要了它,他给了我。我相信他是在通过 SSH 连接到服务器时从一些 bash 代码生成证书的。然后,我在 main
中创建了一个 assets
文件夹,并将证书文件放入其中。我用这行代码 InputStream in = context.getAssets().open("my_cert.crt")
.
得到了输入流
我在 Android 应用程序的团队中工作,被分配了设置用户登录和注册页面的任务。使用 HttpsURLConnection
class 将我的服务器 class 设置为 post 后,我收到了 SSLHandshakeException
错误。
HttpsURL连接码
HttpsURLConnection connection = (HttpsURLConnection) new URL(spec.toString()).openConnection();
经过一些研究,我意识到这是因为 Android 不信任我们的服务器,因为它是自签名的。我正在尝试从 Android 开发站点关注此 documentation,但不明白如何获取 crt
文件。我使用的是 AWS 服务器,但我不是设置自签名证书的人,但我拥有对服务器的完全访问权限。
来自 Android 文档(如何获取 load-der.crt 文件?)
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
这是我获得证书的方式,虽然这个答案可能对其他人帮助不大。我向服务器管理员要了它,他给了我。我相信他是在通过 SSH 连接到服务器时从一些 bash 代码生成证书的。然后,我在 main
中创建了一个 assets
文件夹,并将证书文件放入其中。我用这行代码 InputStream in = context.getAssets().open("my_cert.crt")
.