在本地主机中测试 TLS:给定域的证书无效

Testing TLS in localhost: Certificate is invalid for given domain

我正在尝试在我的个人计算机上使用 spring 启动应用程序 运行 测试 SSL。我使用具有以下参数的 keytool 生成了 PKCS12 证书。

CN = localhost:8080
OU = localhost:8080
O = localhost:8080
L = Galle
S = Galle
C = LK

我将我的应用程序配置为使用此证书并将此自签名证书安装到我的 chrome 浏览器中。

当我尝试使用 chrome 扩展高级 REST 客户端访问我的 API 端点 (https://localhost:8080/api/meta/divisions) 时,我收到一条错误消息

Certificate is invalid for given domain
Certificate presented to the app has different CN (common name) than the domain of the request.

这个错误的原因是什么,我该如何解决这个问题?

当使用 127.0.0.1 作为 CN 并在生成自签名证书时填充 SAN 扩展时,此问题已得到修复。

我试图重现相同的行为,但最初我的 chrome 也阻止了该页面。看起来 google 不允许 localhost:8080 作为 CN 名称。

如果您尝试以下命令:

keytool -genkeypair -keyalg RSA -keysize 2048 -alias server -dname "CN=abcd,OU=efgh,O=ijkl,C=Galle" -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -validity 3650 -keystore identity.jks -storepass secret -keypass secret -deststoretype pkcs12

使用您自己的值将以下 spring 属性添加到您的应用程序:

server:
  port: 8443
  ssl:
    enabled: true
    key-store: classpath:identity.jks
    key-password: secret
    key-store-password: secret

从密钥库导出证书并添加到您的计算机或chrome,它应该可以工作。要提取证书,您可以使用以下命令:

keytool -exportcert -keystore identity.jks -storepass secret -alias server -rfc -file server.cer

你能用上面的步骤重试吗?