问题描述
我有一台带有自签名证书的服务器。此证书已添加到 Android 11 设备上的受信任凭据/用户列表中。我的应用程序和浏览器与它一起正常工作:我可以从 MyServer 通过 Chrome 浏览器下载 pdf 文件。
我的 network-security-config.xml
<network-security-config xmlns:tools="http://schemas.android.com/tools">
<base-config cleartextTrafficPermitted="true">
<trust-anchors tools:ignore="AcceptsUserCertificates">
<!-- Trust preinstalled CAs -->
<certificates src="system" />
<!-- Additionally trust user added CAs -->
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
但是,当我尝试打开 pdf 文件时,我从 PdfViewerActivity 得到了 SSLHandshakeException。 我的代码:
Intent pdfIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.myserver.com/example.pdf"));
startActivity(pdfIntent);
错误日志:
I/ActivityTaskManager:显示 com.google.android.apps.docs/com.google.android.apps.viewer.PdfViewerActivity:+163 毫秒
E/HttpUriOpener: 一般 IOException: SSLHandshakeException
E/PdfViewerActivity:fetchFile:https:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。
是 PdfViewerActivity 内部的错误还是我做错了什么? 如果您有任何解决此问题的方法,我将不胜感激。
UPD:Trust Anchor not found for Android SSL Connection 不是我问题的答案。它不包含有关在 Android 11 上使用 SelfTrusted 证书的信息。
解决方法
是 PdfViewerActivity 内部的错误还是我做错了什么?
都没有。该 Intent
可能会启动数十种可能的 PDF 查看器应用程序。它们都不是您的应用程序,因此它们都不会受到您的网络安全配置的影响。在较新版本的 Android 上,默认情况下会忽略用户安装的证书。
您需要自己下载 PDF 文件,然后使用 FileProvider
及其 getUriForFile()
方法获得一个 Uri
以与您的 Intent
一起使用。
或者,您可以将服务器切换为使用常规 SSL 证书(例如,Let's Encrypt)而不是自签名证书,如果这对您的情况而言可行。