自签名证书仅适用于本地主机,不适用于 127.0.0.1

Rya*_*hel 4 ssl https openssl node.js secure-context

我正在尝试生成一个自签名证书,以便我的本地开发环境使用 HTTPS,但我遇到了一些问题。这样做的原因是我想通过我的本地网络(通过我的本地 IP 192.168.1.155)在我的手机上测试推送通知,并且通知只能通过安全上下文工作。

它似乎只在我去的时候工作localhost:8080,并且在导航到时仍然不安全127.0.0.1:8080。当我导航到127.0.0.1:8080Chrome 的安全页面时说:This site is missing a valid, trusted certificate (net::ERR_CERT_COMMON_NAME_INVALID).

这是我用来生成证书的设置:

请求.cnf:

[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
C = US
ST = VA
L = SomeCity
O = MyCompany
OU = MyDivision
CN = 127.0.0.1
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = 127.0.0.1
DNS.3 = 192.168.1.155
Run Code Online (Sandbox Code Playgroud)

openssl req -newkey rsa:2048 -x509 -nodes -keyout key.pem -new -out cert.pem -config req.cnf -sha256 -days 3650

我想可能是我的CNalt_names不正确的,但我不确定将它们更改为什么以使站点始终安全运行(通过 localhost、127.0.0.1 或 192.168.1.155)

Rya*_*hel 5

在一个意外的橡皮鸭调试案例中,我似乎在发布后暂时解决了这个问题。这是我所做的:

请求.cnf:

[req]
distinguished_name = req_distinguished_name
x509_extensions = v3_req
prompt = no
[req_distinguished_name]
C = US
ST = VA
L = SomeCity
O = MyCompany
OU = MyDivision
CN = localhost
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = DNS:localhost,IP:192.168.1.155,IP:127.0.0.1
Run Code Online (Sandbox Code Playgroud)

命令提示符:

openssl req -newkey rsa:2048 -x509 -nodes -keyout key.pem -new -out cert.pem -config req.cnf -sha256 -days 3650
Run Code Online (Sandbox Code Playgroud)

然后导航到 Chrome 中的页面,将证书(因为它仍然无效)保存为 DER 文件,然后使用mmc.exe,将其导入您机器上的受信任的根证书颁发机构(假设您使用的是 Windows)

  • 为了解释为什么你的橡皮鸭调试成功:在你的原始证书中,你在 subjectAltName 中将 127.0.0.1 定义为 DNS 类型,而不是 IP 类型。新证书已修复此问题。 (3认同)