我已经为我的应用程序的 admin
实例和我的应用程序的 judge
实例生成了自签名证书。这些实例在不同的机器上运行,它们都有彼此证书和自己证书的副本。我想在这两者之间进行通信,我想知道我目前的做法是否是正确的做法:
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate certificate = cf.generateCertificate(new FileInputStream(...));
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, "test".toCharArray());
keyStore.setCertificateEntry("admin", certificate);
// Code omitted which repeats the above to set the judge certificate
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
factory = ctx.getSocketFactory(); // Or #getServerSocketFactory() if admin and not judge
有了这个,我将能够安全地与两个实例通信,对吗?
最佳答案
没有。 KeyManager 需要一个带有 key 条目的 keystore ,而不是证书条目。只需按照 JSSE 设计者的意图使用 keystore 文件即可。
关于java - SSL 通信 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378790/