发布于 3年前

给本地localhost域名添加https证书

本文介绍如何给本地域名localhost添加证书,但此方法仅限在开发环境使用。在生产环境中,强烈禁止使用自签名证书

创建认证中心(Certificate authority,CA)

生成RootCA.pem, RootCA.key 以及 RootCA.crt:

openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=US/CN=Example-Root-CA"
openssl x509 -outform pem -in RootCA.pem -out RootCA.crt

Example-Root-CA是一个用例的名称,实际使用中可以把它为你自己要的名字。

域名证书

假设有两个本地机器域名fake1.local和fake2.local,这两个域名使用hosts文件将其指向127.0.0.1。

创建domains.ext列出所有的本地域名:

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
DNS.2 = fake1.local
DNS.3 = fake2.local

生成localhost.key, localhost.csr以及localhost.crt文件:

openssl req -new -nodes -newkey rsa:2048 -keyout localhost.key -out localhost.csr -subj "/C=CN/ST=Guandong/L=Shenzhen/O=Example-Certificates/CN=localhost.local"
openssl x509 -req -sha256 -days 1024 -in localhost.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -extfile domains.ext -out localhost.crt

示例里的省份,城市可以替换为自己的地址。

配置webserver,这里的示例使用的是apache:

SSLEngine on
SSLCertificateFile "C:/example/localhost.crt"
SSLCertificateKeyFile "C:/example/localhost.key"

信任本地CA

站点加载有关自签名证书时会有警告。为了获得绿色锁,必须将新的本地CA添加到受信任的根证书颁发机构。

Windows 10: Chrome, IE11 以及 Edge

Windows 10是能识别.crt文件,右键RootCA.crt文件,然后执行安装,就会弹出导入证书的窗口。

这样Chrome,IE11以及Edge就会显示绿色锁。

Window10:Firefox

对于Firefox,有两种方法来让浏览器信任我们自签名的证书:

1、在浏览器的地址栏,打开about:config,把security.enterprise_roots.enabled 设置为true。

2、在浏览器导入证书:

在地址栏输入about:preferences#privacy > 证书(Certificats) > 导入(Import) > RootCA.pem > 确定网站
©2020 edoou.com   京ICP备16001874号-3