gpt4 book ai didi

java - 无法在 cacert 中导入证书

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

我有 JAVA_HOME=C:\Users\myuser\jdk1.8.0_65

Eclipse 中的 JRE 系统库指向 C:\Users\myuser\jdk1.8.0_65

java -version 给出 C:\Users\myuser\jdk1.8.0_65

通过以下方式导入证书:

C:\Users\myuser\jdk1.8.0_65\bin\keytool.exe -importcert -trustcacerts -keystore C:\Users\myuser\jdk1.8.0_65\jre\lib\security\cacerts -storepass changeit -noprompt -alias ldap -file C:\Users\myuser\certificates\ldap_cer.cer

Keytool 列表给出:

C:\Users\myuser>keytool -list
Enter keystore password:
Keystore type: JKS
Keystore provider: SUN

Your keystore contains 0 entries

当我尝试使用以下程序检查证书时,我可以看到条目,为什么 keytool -list 没有给我相同的结果?如何添加证书?我缺少什么?

package com.;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.util.Enumeration;
public class HandShake {
static FileInputStream is;
public static void main(String[] args) {
try {

File file = new File("C://Users//myuser//jdk1.8.0_65//jre//lib//security//cacerts");
System.setProperty("javax.net.ssl.keyStore", file.getAbsolutePath());
System.out.println(System.getProperty("javax.net.ssl.keyStore"
));
is= new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "changeit";
keystore.load(is, password.toCharArray());
Enumeration enumeration = keystore.aliases();
while(enumeration.hasMoreElements()) {
String alias = (String)enumeration.nextElement();
if(alias.equals("ldap")){

System.out.println("alias name: " + alias);
Certificate certificate = keystore.getCertificate(alias);
System.out.println(certificate.toString());
}
}

} catch (java.security.cert.CertificateException e4) {
e4.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (KeyStoreException e3) {
e3.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != is)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

最佳答案

可能您正在尝试在另一个 keystore 上运行 keystore -list

尝试

keytool -list -keystore C:\Users\myuser\jdk1.8.0_65\jre\lib\security\cacerts

已编辑

SSL 属性是通过系统属性在 JVM 级别设置的。这意味着您可以在运行程序时设置它们 (java -D....),或者您可以通过执行 System.setProperty 在代码中设置它们。

The specific keys you have to set are below:

javax.net.ssl.keyStore- Location of the Java keystore file containing an application process's own certificate and private key. On Windows, the specified pathname must use forward slashes, /, in place of backslashes.

javax.net.ssl.keyStorePassword - Password to access the private key from the keystore file specified by javax.net.ssl.keyStore. This password is used twice: To unlock the keystore file (store password), and To decrypt the private key stored in the keystore (key password).

javax.net.ssl.trustStore - Location of the Java keystore file containing the collection of CA certificates trusted by this application process (trust store). On Windows, the specified pathname must use forward slashes, /, in place of backslashes, .

If a trust store location is not specified using this property, the SunJSSE implementation searches for and uses a keystore file in the following locations (in order):

$JAVA_HOME/lib/security/jssecacerts $JAVA_HOME/lib/security/cacerts javax.net.ssl.trustStorePassword - Password to unlock the keystore file (store password) specified by javax.net.ssl.trustStore.

javax.net.ssl.trustStoreType - (Optional) For Java keystore file format, this property has the value jks (or JKS). You do not normally specify this property, because its default value is already jks.

javax.net.debug - To switch on logging for the SSL/TLS layer, set this property to ssl.

参见 java SSL and cert keystore

关于java - 无法在 cacert 中导入证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44021736/

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