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

时间:2020-02-02 21:45:54

标签: node.js ssl https openssl secure-context

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

它似乎仅在我转到localhost:8080时有效,而在导航到127.0.0.1:8080时仍然不安全。当我导航到127.0.0.1:8080时,Chrome的安全页面显示:This site is missing a valid, trusted certificate (net::ERR_CERT_COMMON_NAME_INVALID).

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

req.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

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

我想我的CNalt_names可能不正确,但是我不确定将其更改为什么,以便站点将始终安全地运行(通过本地主机127.0.0.1) ,或192.168.1.155)

1 个答案:

答案 0 :(得分:1)

在一个无法预料的橡皮鸭调试案例中,我似乎终于在发布此问题后立即解决了这个问题。这是我所做的:

req.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

命令提示符:

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

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