Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 自签名证书仅适用于本地主机,而不是127.0.0.1_Node.js_Ssl_Https_Openssl_Secure Context - Fatal编程技术网

Node.js 自签名证书仅适用于本地主机,而不是127.0.0.1

Node.js 自签名证书仅适用于本地主机,而不是127.0.0.1,node.js,ssl,https,openssl,secure-context,Node.js,Ssl,Https,Openssl,Secure Context,我正在尝试生成一个自签名证书,以便我的本地开发环境使用HTTPS,但我遇到了一些问题。原因是我想通过本地网络(通过我的本地IP192.168.1.155)在手机上测试推送通知,而通知只能通过安全上下文工作 它似乎只在我转到localhost:8080时起作用,在导航到127.0.0.1:8080时仍然不安全。当我导航到127.0.0.1:8080时,Chrome的安全页面显示:此站点缺少有效的可信证书(net::ERR\u CERT\u COMMON\u NAME\u无效)。 以下是我用来生成证

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

它似乎只在我转到
localhost:8080
时起作用,在导航到
127.0.0.1:8080
时仍然不安全。当我导航到
127.0.0.1:8080时,Chrome的安全页面显示:
此站点缺少有效的可信证书(net::ERR\u CERT\u COMMON\u NAME\u无效)。

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

请求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
[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


我想我的
CN
alt_name
可能是不正确的,但我不确定该如何更改它们以使站点始终安全地工作(通过localhost、127.0.0.1或192.168.1.155)

在一个不可预见的橡皮鸭调试案例中,我似乎在发布它之后,终于暂时解决了这个问题。以下是我所做的:

请求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
[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)

解释橡皮鸭调试成功的原因:在原始证书中,您在subjectAltName中将127.0.0.1定义为DNS类型,而不是IP类型。这是用新证书修复的。