简体   繁体   English

java SSL和cert密钥库

[英]java SSL and cert keystore

How does my java program know where my keystore containing the certificate is? 我的java程序如何知道包含证书的密钥库在哪里? Or alternatively how do I tell my java program where to look for the keystore? 或者我如何告诉我的java程序在哪里查找密钥库?

After specifying the keystore in some way, how to specify the certificate to use for authenticating the server to client? 以某种方式指定密钥库后,如何指定用于向客户端验证服务器的证书?

SSL properties are set at the JVM level via system properties. SSL属性通过系统属性在JVM级别设置。 Meaning you can either set them when you run the program (java -D....) Or you can set them in code by doing System.setProperty. 这意味着您可以在运行程序时设置它们(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. javax.net.ssl.keyStore - 包含应用程序进程自己的证书和私钥的Java密钥库文件的位置。 On Windows, the specified pathname must use forward slashes, /, in place of backslashes. 在Windows上,指定的路径名​​必须使用正斜杠/,而不是反斜杠。

javax.net.ssl.keyStorePassword - Password to access the private key from the keystore file specified by javax.net.ssl.keyStore. javax.net.ssl.keyStorePassword - 从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). javax.net.ssl.trustStore - 包含此应用程序进程信任的CA证书集合(信任库)的Java密钥库文件的位置。 On Windows, the specified pathname must use forward slashes, / , in place of backslashes, \\ . 在Windows上,指定的路径名​​必须使用正斜杠/ ,代替反斜杠\\

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): 如果未使用此属性指定信任库位置,则SunJSSE实现将在以下位置(按顺序)搜索并使用密钥库文件:

  1. $JAVA_HOME/lib/security/jssecacerts
  2. $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.trustStorePassword - 解锁javax.net.ssl.trustStore指定的密钥库文件(存储密码)的密码。

javax.net.ssl.trustStoreType - (Optional) For Java keystore file format, this property has the value jks (or JKS). javax.net.ssl.trustStoreType - (可选)对于Java密钥库文件格式,此属性的值为jks(或JKS)。 You do not normally specify this property, because its default value is already jks. 您通常不指定此属性,因为其默认值已经是jks。

javax.net.debug - To switch on logging for the SSL/TLS layer, set this property to ssl. javax.net.debug - 要打开SSL / TLS层的日志记录,请将此属性设置为ssl。

System.setProperty("javax.net.ssl.trustStore", path_to_your_jks_file);

Just a word of caution. 只是提醒一句。 If you are trying to open an existing JKS keystore in Java 9 onwards, you need to make sure you mention the following properties too with value as "JKS": 如果您尝试在Java 9之后打开现有的JKS密钥库,则需要确保提及以下属性,其值为“JKS”:

javax.net.ssl.keyStoreType
javax.net.ssl.trustStoreType

The reason being that the default keystore type as prescribed in java.security file has been changed to pkcs12 from jks from Java 9 onwards. 原因是java.security文件中规定的默认密钥库类型已从Java 9开始从jks更改为pkcs12。

First of all, there're two kinds of keystores. 首先,有两种密钥库。

Individual and General 个人一般

The application will use the one indicated in the startup or the default of the system. 应用程序将使用启动或系统默认中指示的应用程序。

It will be a different folder if JRE or JDK is running, or if you check the personal or the "global" one. 如果JRE或JDK正在运行,或者您检查个人或“全局”文件夹,它将是一个不同的文件夹。

They are encrypted too 它们也是加密的

In short, the path will be like: 简而言之,路径将是:

$JAVA_HOME/lib/security/cacerts for the "general one", who has all the CA for the Authorities and is quite important. $JAVA_HOME/lib/security/cacerts为“general one”,它拥有权威机构的所有CA并且非常重要。

you can also mention the path at runtime using -D properties as below 您还可以使用-D属性在运行时提及路径,如下所示

-Djavax.net.ssl.trustStore=/home/user/SSL/my-cacerts 
-Djavax.net.ssl.keyStore=/home/user/SSL/server_keystore.jks

In my apache spark application, I used to provide the path of certs and keystore using --conf option and extraJavaoptions in spark-submit as below 在我的apache spark应用程序中,我曾经在spark-submit中使用--conf选项和extraJavaoptions提供了certs和keystore的路径,如下所示

--conf 'spark.driver.extraJavaOptions= 
-Djavax.net.ssl.trustStore=/home/user/SSL/my-cacerts 
-Djavax.net.ssl.keyStore=/home/user/SSL/server_keystore.jks' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我真的需要手动将SSL证书导入Java密钥库吗? - Do I Really Need to Import a SSL Cert into Java Keystore Manually? Java SSL connect,以编程方式将服务器证书添加到密钥库 - Java SSL connect, add server cert to keystore programmatically 密钥库证书链长度错误 - 如何以编程方式将整个证书链加载到 Java 密钥库中,以便使用 SSL 身份验证连接到 Kafka? - Keystore cert chain wrong length - How to load entire cert chain into Java keystore programmatically for connecting to Kafka with SSL auth? Java证书和密钥库之间的关系 - Relationship between Java cert and keystore HTTPS(SSL)请求从没有库的密钥库中读取证书 - HTTPS (SSL) Request Reading Cert From Keystore Without a Library 应用程序中的Java SSL和密钥库密码 - Java SSL and keystore password in application GoDaddy SSL证书不使用Java - GoDaddy SSL Cert Not Working With Java JDBC + SSL:将CA证书,客户端证书和客户端捆绑到一个密钥库文件中 - JDBC+SSL: Bundling a CA cert, client cert and client into a single keystore file Java密钥库-从PKS文件导入现有的通配符证书 - Java Keystore - Importing existing wildcard cert from a PKS file 将证书导入Java密钥库,JVM会忽略新的证书 - Imported certificate to Java keystore, JVM ignores the new cert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM