当我执行以下代码时:
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(null, null);
然后在随后建立ssl连接时,我得到一个Exception in thread "main"javax.net.ssl.SSLHandshakeException: Received fatalalert:handshake_failure
而不是NullPointerException或其他什么。那么使用哪个 keystore 呢?还是 NPE 被隐藏了?
最佳答案
当null
用作KeyManagerFactory.init(..., ...)
的 keystore 参数时,不使用 keystore (实际上在内部使用an empty list) )。
不存在这样的“隐藏”NullPointerException
,也没有理由应该有这样一个。当然,在尝试使用 key 管理器时,此处使用 null 值和空集合会产生后果,但并非所有 SSL/TLS 连接都需要 keystore (例如,没有客户端证书或匿名密码套件的客户端) )。
(请注意,尽管 keystore 没有默认值 there is one for the truststore ,因此该默认值将与 TrustManagerFactory.init(null)
一起使用。)
关于java - 当 null 传递给 Java 中的 KeyManagerFactory.init() 时,使用哪个 keystore ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31292322/