如何将自签名证书添加到 HttpsURLConnection 中的默认证书
How to ADD self-signed certificates to default certificates in HttpsURLConnection
早上好。
我的问题是,我想创建一个 HttpsURLConnection
接受默认的受信任证书以及我的自签名证书。
上网查了一下,发现了很多类似的问题,但都不是我想要的。
特别好的代码是here。
这正是我想要的,但有一点不同:
他们只会信任自签名证书,我也想信任自签名证书。
基本上我不想从一个空的 KeyStore
开始,我想获得默认的密钥库,whatever/wherever 是的,我的 [=] 中已经包含默认证书的东西32=].
PS:我在Android工作。
感谢大家
您基本上想要使用默认的可信证书以及您自己的可信证书。此处提供了类似的问题和答案:Registering multiple keystores in JVM
我也运行遇到了同样的问题,找到了Code a Ray really useful. After using his code snippet for multiple projects I created a library out of it. You can find it here: sslcontext-kickstart
的答案
对于您的用例,以下将您的证书包裹在信任库文件中的剪接器应该可以解决问题:
SSLFactory sslFactory = SSLFactory.builder()
.withDefaultTrustMaterial()
.withTrustMaterial("my-truststore.jks", "password".toCharArray())
.build();
HttpsURLConnection httpsURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpsURLConnection.setHostnameVerifier(sslFactory.getHostnameVerifier());
httpsURLConnection.setSSLSocketFactory(sslFactory.getSslContext().getSocketFactory());
早上好。
我的问题是,我想创建一个 HttpsURLConnection
接受默认的受信任证书以及我的自签名证书。
上网查了一下,发现了很多类似的问题,但都不是我想要的。
特别好的代码是here。
这正是我想要的,但有一点不同: 他们只会信任自签名证书,我也想信任自签名证书。
基本上我不想从一个空的 KeyStore
开始,我想获得默认的密钥库,whatever/wherever 是的,我的 [=] 中已经包含默认证书的东西32=].
PS:我在Android工作。
感谢大家
您基本上想要使用默认的可信证书以及您自己的可信证书。此处提供了类似的问题和答案:Registering multiple keystores in JVM
我也运行遇到了同样的问题,找到了Code a Ray really useful. After using his code snippet for multiple projects I created a library out of it. You can find it here: sslcontext-kickstart
的答案对于您的用例,以下将您的证书包裹在信任库文件中的剪接器应该可以解决问题:
SSLFactory sslFactory = SSLFactory.builder()
.withDefaultTrustMaterial()
.withTrustMaterial("my-truststore.jks", "password".toCharArray())
.build();
HttpsURLConnection httpsURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpsURLConnection.setHostnameVerifier(sslFactory.getHostnameVerifier());
httpsURLConnection.setSSLSocketFactory(sslFactory.getSslContext().getSocketFactory());