gpt4 book ai didi

android - 无法在 CouchbaseLiteHttpClientFactory Android 中添加 SSL 证书

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

我的 Android 应用程序有 Couch Base Lite Replication。使用早期版本我能够添加 SSL 证书和同步,但是当我更新到版本 1.3.1 时,这些类已被弃用,现在我无法添加证书。有什么帮助吗?

最佳答案

找到解决方案

CertificateFactory cf;
try {
cf = CertificateFactory.getInstance("X.509");

AssetManager assManager = Utils.context.getAssets();
InputStream is = null;
is = assManager.open("certificate.cer");
InputStream caInput = new BufferedInputStream(is);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);

System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}

// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// create CouchbaseLiteHttpClientFactory
CouchbaseLiteHttpClientFactory cblHttpClientfactory = new CouchbaseLiteHttpClientFactory
(database.getPersistentCookieStore());

// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
cblHttpClientfactory.setSSLSocketFactory(context.getSocketFactory());
manager.setDefaultHttpClientFactory(cblHttpClientfactory);

} catch (CertificateException e) {
}

关于android - 无法在 CouchbaseLiteHttpClientFactory Android 中添加 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799343/

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