Java (JSSE HostnameChecker)Java
错误信息
No subject alternative names matching IP address 127.0.0.1 found
也可能输出为
- java.security.cert.CertificateException: No subject alternative names matching IP address 127.0.0.1 found
其中的地址就是你 URL 中的地址;JDK 21 及更高版本会在异常文本前加上“(certificate_unknown) ”。对于 DNS 主机名,消息为“No subject alternative DNS name matching <host> found.”。
Java (JSSE HostnameChecker) — URL host is an IP address that is not an IP SAN of the certificate
含义
URL 中的主机是 IP 地址,而证书的主题备用名称(SAN)扩展中没有与它相等的 IP 地址条目。Java(依据 RFC 6125)只把 IP 地址与 IP SAN 条目比较;DNS 名称和通用名称永远不会与地址匹配。
原因
客户端通过 IP 地址连接
常见存储 IP 地址而非主机名的配置文件、JDBC URL 和服务注册中心。
发往 Pod 或节点地址的集群内部流量
常见服务发现分发的是 IP 地址(Pod、节点、云实例),而证书中写的是服务名。
证书本来就是为通过 IP 访问准备的,却缺少 IP SAN
偶见把 IP 地址放进通用名称是行不通的;它必须是 subjectAltName 中的 IP 条目。
修复方法
- 1.
使用证书中列出的 DNS 名称连接
使用主机名,并让它解析到所需的地址;这样主机名验证就能通过。
bash # Use the DNS name from the certificate; map it to the IP where needed echo "10.0.0.5 api.example.com" | sudo tee -a /etc/hosts curl -sI https://api.example.com/ - 2.
签发带 IP SAN 的证书
如果客户端必须使用地址,就把它作为 IP 条目加入(公共 CA 很少签发这类证书;私有 CA 可以)。
bash # Or issue the certificate with the IP address as an IP SAN entry openssl req -new -key server.key -out server.csr -subj "/CN=api.example.com" \ -addext "subjectAltName=DNS:api.example.com,IP:10.0.0.5" keytool -certreq -alias server -keystore server.p12 -ext "SAN=dns:api.example.com,ip:10.0.0.5" -file server.csr