我正在开发适用于Android 5或更高版本的应用。
我需要加载SSL客户端证书才能向API发出请求。
我可以从downloads文件夹加载证书并发出请求:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "certificate.pfx");
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(file), "password".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, "password".toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kms, null, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
但我想知道是否有可能从Android系统加载证书(当然,如果我以前在Android中安装了这样的post)。
有可能吗?
答案 0 :(得分:0)
将您的证书文件添加到资产目录并替换:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "certificate.pfx");
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(file), "password".toCharArray());
使用
KeyStore clientStore = KeyStore.getInstance("PKCS12");
InputStream is = context.getAssets().open("certificate.pfx");
clientStore.load(is, "password".toCharArray());