如何将。p12证书从类路径导入到java证书仓库中?首先,我使用了InstallCert https://code.google.com/p/java-use-examples/source/browse/trunk/src/com/aw/ad/util/InstallCert.java并进行了一些更改,以便将服务器证书导入到java安装目录中的密钥存储库中。这工作得很好,但现在我想从我的类路径加载证书。
编辑:我只是使用。cer证书,见下一个答案
答案:
InputStream certIn = ClassLoader.class.getResourceAsStream("/package/myCert.cer");
final char sep = File.separatorChar;
File dir = new File(System.getProperty("java.home") + sep + "lib" + sep + "security");
File file = new File(dir, "cacerts");
InputStream localCertIn = new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(localCertIn, passphrase);
if (keystore.containsAlias("myAlias")) {
certIn.close();
localCertIn.close();
return;
}
localCertIn.close();
BufferedInputStream bis = new BufferedInputStream(certIn);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
Certificate cert = cf.generateCertificate(bis);
keystore.setCertificateEntry("myAlias", cert);
}
certIn.close();
OutputStream out = new FileOutputStream(file);
keystore.store(out, passphrase);
out.close();
对于Java Web Start,不要使用ClassLoader,使用Class本身:
InputStream certIn = Certificates.class.getResourceAsStream("/package/myCert.cer");
我遇到"java.io. net "。FileNotFoundException: C:Program Files (x86)Java jre1.8.0_45libsecuritycacerts (Access is denied)"问题。我必须进入文件夹 securityy并手动授予访问h