jww*_*jww 0 java keystore x509certificate
我正在尝试给这只猫剥皮:Use PEM Encoded CA Cert on filesystem direct for HTTPS request? 其他方式。
Java有一个类KeyStore.TrustedCertificateEntry,但我不知道如何将证书加载到其中。我的代码类似于以下内容:
import java.security.KeyStore.TrustedCertificateEntry;
...
X509Certificate ca = (X509Certificate) CertificateFactory(...);
KeyStore ks = TrustedCertificateEntry(ca);
Run Code Online (Sandbox Code Playgroud)
和:
X509Certificate ca = (X509Certificate) CertificateFactory(...);
KeyStore ks = KeyStore.TrustedCertificateEntry(ca);
Run Code Online (Sandbox Code Playgroud)
和:
X509Certificate ca = (X509Certificate) CertificateFactory(...);
KeyStore ks = new KeyStore.TrustedCertificateEntry(ca);
Run Code Online (Sandbox Code Playgroud)
和:
X509Certificate ca = (X509Certificate) CertificateFactory(...);
KeyStore ks = new KeyStore.TrustedCertificateEntry(ca);
Run Code Online (Sandbox Code Playgroud)
该程序无法编译,并出现类似以下内容的错误:
SuperCert.java:33: error: cannot find symbol
KeyStore ks = TrustedCertificateEntry(ca);
^
symbol: method TrustedCertificateEntry(X509Certificate)
location: class TestCert
Run Code Online (Sandbox Code Playgroud)
将我的 X509 证书加载到 后KeyStore,我计划在 a 中使用它TrustManagerFactory并最终使用HttpsURLConnection.
如何将 a 加载X509Certificate到 a 中TrustedCertificateEntry?
我根据 Vit Hnilica 在 《loading acertificate from keystore》中的回答找到了它。我将用这个答案留下问题,因为大多数 Stack Overflow 答案都以“转换为openssl,然后使用keytool...”开头。
向 Vit 发布这个答案致敬。Hnilica 的答案是我在 Stack Overflow 上浏览了很多类似问题和答案后找到的唯一答案。
String CA_FILE = ...;
FileInputStream fis = new FileInputStream(CA_FILE);
X509Certificate ca = (X509Certificate) CertificateFactory.getInstance(
"X.509").generateCertificate(new BufferedInputStream(fis));
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setCertificateEntry(Integer.toString(1), ca);
TrustManagerFactory tmf = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3687 次 |
| 最近记录: |