您的位置:首页 > 其它

解决自签名证书在Chrome上的“不是私密连接问题”

2018-03-05 19:52 696 查看
有时候需要在局域网上访问IP地址,但是由于操作系统或者浏览器的原因(苹果,小程序不允许在app中访问http接口),会给弹出警告,如下:



此问题可用openssl生成证书,并在浏览器中或者安卓苹果设备上安装证书来解决。

相关概念

生成证书

一.创建根证书

新建
MyCompanyCA.cnf
文件并输入以下内容:

[ req ]
distinguished_name  = req_distinguished_name
x509_extensions     = root_ca

[ req_distinguished_name ]

# 以下内容可随意填写
countryName             = CN (2 letter code)
countryName_min         = 2
countryName_max         = 2
stateOrProvinceName     = ZheJiang
localityName            = HangZhou
0.organizationName      = Mycompany
organizationalUnitName  = technology
commonName              = develop
commonName_max          = 64
emailAddress            = xxxxxxxx@gmail.com
emailAddress_max        = 64

[ root_ca ]
basicConstraints            = critical, CA:true


新建
MyCompanyLocalhost.ext
文件并输入以下内容:

subjectAltName = @alt_names
extendedKeyUsage = serverAuth

[alt_names]

# 域名,如有多个用DNS.2,DNS.3…来增加
DNS.1 = domain.com
# IP地址
IP.1 = 192.168.2.221
IP.2 = 127.0.0.1


说明:

MyCompanyCA.cnf
文件是为申请CA根证书的配置文件;

MyCompanyLocalhost.ext
是生成服务器证书的扩展配置文件;

二. 生成证书

执行以下3条命令:

openssl req -x509 -newkey rsa:2048 -out MyCompanyCA.cer -outform PEM -keyout MyCompanyCA.pvk -days 10000 -verbose -config MyCompanyCA.cnf -nodes -sha256 -subj "/CN=MyCompany CA"

openssl req -newkey rsa:2048 -keyout MyCompanyLocalhost.pvk -out MyCompanyLocalhost.req -subj /CN=localhost -sha256 -nodes
openssl x509 -req -CA MyCompanyCA.cer -CAkey MyCompanyCA.pvk -in MyCompanyLocalhost.req -out MyCompanyLocalhost.cer -days 10000 -extfile MyCompanyLocalhost.ext -sha256 -set_serial 0x1111


执行完成后,会得到,其中
MyCompanyCA.cer
是用来安装在浏览器、安卓和苹果设备上,
MyCompanyLocalhost.cer
MyCompanyLocalhost.pvk
是放在服务端的证书和key文件,在Nginx中配置即可.

-rw-r--r--  1 hugh  staff   1.0K  3  5 15:20 MyCompanyCA.cer
-rw-r--r--  1 hugh  staff   592B  3  5 16:03 MyCompanyCA.cnf
-rw-r--r--  1 hugh  staff   1.6K  3  5 15:20 MyCompanyCA.pvk
-rw-r--r--  1 hugh  staff   1.0K  3  5 15:20 MyCompanyLocalhost.cer
-rw-r--r--  1 hugh  staff   116B  3  5 15:20 MyCompanyLocalhost.ext
-rw-r--r--  1 hugh  staff   1.6K  3  5 15:20 MyCompanyLocalhost.pvk
-rw-r--r--  1 hugh  staff   891B  3  5 15:20 MyCompanyLocalhost.req


在设备上安装证书

1、以mac为例,直接双击
MyCompanyCA.cer
文件或者在钥匙串中导入证书,证书导入后,默认状态如下:



打开“信任”并选择“永远相信”

打开要访问的地址:已经变为绿色”安全”



参考:

1. 生成证书文件

2. https+ip地址的证书说明
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息