【问题标题】:Getting java.security.cert.CertificateException, however the certificate is imported into the truststore获取 java.security.cert.CertificateException,但是证书被导入到信任库
【发布时间】:2014-03-24 13:31:56
【问题描述】:

我总是收到一个

java.security.cert.CertificateException: No subject alternative names present

例外,但是我已将证书导入到我的 trusrstore。这是我的设置:

我正在使用基于 com.sun.net.httpserver.HttpsServer 的小型 HTTPS 服务器。我创建了一个带有自签名证书的密钥库:

keytool -genkey -keyalg RSA -alias myCert -keystore keystore.jks -storepass myPass -validity 360 -keysize 2048 

我将路径和密码作为 VM 参数传递给密钥库 Djavax.net.ssl.keyStore=/tmp/truststore.jks Djavax.net.ssl.keyStorePassword=myPass

如果我运行openssl s_client -connect 192.168.1.101:4443,我可以看到它使用了正确的证书。

为了信任客户端站点上的证书,我已从服务器密钥库导出证书并将其导入客户端信任库:

  keytool -export -alias myCert -file myCert.crt -keystore keystore.jks
  keytool -import -trustcacerts -alias -file myCert.crt -keystore truststore.jks

对于客户,我使用了一个使用 jersey 的小型测试程序。我将信任库的路径作为 VM 参数 -Djavax.net.ssl.trustStore=/tmp/truststore.jks

public class Tester {
    public static void main(String[] args) {

        Client client = ClientBuilder.newClient();
        try {
            String name = client.target("https://192.168.1.101:4443")
                    .request(MediaType.TEXT_PLAIN)
                    .get(String.class);

            System.out.println(name);
        } catch (final Exception e) {
            System.err.println(e);
        }
    }
}

但是无论自签名证书在信任库中,我总是得到

  java.security.cert.CertificateException: No subject alternative names present
exception

这可能是什么原因?

【问题讨论】:

    标签: java https ssl-certificate keystore truststore


    【解决方案1】:

    HTTPS 证书应包含 SubjectAltName 扩展名,其值应为 IP 地址(在您的情况下为 192.168.1.101)或主机的 DNS 名称。

    【讨论】:

    • 当我将域而不是名称放在 CN 中,然后使用本地 DNS 服务器将域解析为我的本地 IP 地址时,它可以工作。但在那种情况下,我不需要设置 SubjectAltName。
    猜你喜欢
    • 2021-08-28
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2017-02-19
    • 2021-01-01
    • 2012-07-05
    相关资源
    最近更新 更多