让Chrome接受自签名的本地主机证书

让Chrome接受自签名的本地主机证书

技术背景

在开发和测试过程中,我们经常会使用自签名的SSL/TLS证书。然而,Chrome浏览器出于安全考虑,默认情况下不会接受这些自签名证书,会提示“您的连接不是私密连接”等错误信息。为了在开发环境中顺利使用HTTPS,我们需要让Chrome接受这些自签名证书。

实现步骤

仅适用于localhost(Chrome 119及以上)

  1. 在Chrome浏览器中访问 chrome://flags/#temporary-unexpire-flags-m118
  2. 找到高亮显示的文本:“Temporarily unexpire flags that expired as of M118. These flags will be removed soon. – Mac, Windows, Linux, ChromeOS, Android, Fuchsia, Lacros”。
  3. 点击“Enable”,然后重启Chrome。

仅适用于localhost(Chrome 118及以下)

  1. 在Chrome浏览器中访问 chrome://flags/#allow-insecure-localhost
  2. 找到高亮显示的文本:“Allow invalid certificates for resources loaded from localhost”。
  3. 点击“Enable”。

其他站点的处理方法

  • 尝试在窗口的任意位置输入 thisisunsafe,浏览器应该会允许你访问该页面。
  • 对于本地自签名证书,可以使用 mkcert 工具,避免复杂的命令和手动步骤。

使用openssl命令生成证书

  1. 复制以下代码片段到一个新文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
######################
# Become a Certificate Authority
######################

# Generate private key
openssl genrsa -des3 -out myCA.key 2048
# Generate root certificate
openssl req -x509 -new -nodes -key myCA.key -sha256 -days 825 -out myCA.pem

######################
# Create CA-signed certs
######################

NAME=mydomain.example # Use your own domain name
# Generate a private key
openssl genrsa -out $NAME.key 2048
# Create a certificate-signing request
openssl req -new -key $NAME.key -out $NAME.csr
# Create a config file for the extensions
>$NAME.ext cat <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = $NAME # Be sure to include the domain name here because Common Name is not so commonly honoured by itself
DNS.2 = bar.$NAME # Optionally, add additional domains (I've added a subdomain here)
IP.1 = 192.168.0.13 # Optionally, add an IP address (if the connection which you have planned requires it)
EOF
# Create the signed certificate
openssl x509 -req -in $NAME.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial \
-out $NAME.crt -days 825 -sha256 -extfile $NAME.ext
  1. 更新变量 NAME(可选更新 DNS.2IP.1)并保存文件。
  2. 运行脚本(例如 bash generate_certs.sh),这将生成 myCA.pem$NAME.crt$NAME.key
  3. 在Chrome设置中,将生成的CA证书(myCA.pem)作为“Authority”导入(不是导入“Your Certificates”):设置 -> 管理证书 -> 受信任的根证书颁发机构 -> 导入。
  4. 在服务器中使用生成的 $NAME.crt$NAME.key 文件进行SSL/TLS配置。

Mac系统的额外步骤

  1. 在“文件 -> 导入文件”中导入CA证书,然后在列表中找到它,右键单击,展开“信任”,选择“始终信任”。
  2. basicConstraints=CA:FALSE 下方添加 extendedKeyUsage=serverAuth,clientAuth,并确保在设置时将“CommonName”设置为与 $NAME 相同。

Windows系统的额外步骤

  1. myCA.pem 转换为 myCA.pfx
1
openssl pkcs12 -export -out myCA.pfx -inkey myCA.key -in myCA.pem
  1. 通过双击 myCA.pfx 文件,将其导入到Windows的受信任根证书颁发机构中,选择“本地计算机”,然后按照向导完成导入。

核心代码

openssl生成证书代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
######################
# Become a Certificate Authority
######################

# Generate private key
openssl genrsa -des3 -out myCA.key 2048
# Generate root certificate
openssl req -x509 -new -nodes -key myCA.key -sha256 -days 825 -out myCA.pem

######################
# Create CA-signed certs
######################

NAME=mydomain.example # Use your own domain name
# Generate a private key
openssl genrsa -out $NAME.key 2048
# Create a certificate-signing request
openssl req -new -key $NAME.key -out $NAME.csr
# Create a config file for the extensions
>$NAME.ext cat <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = $NAME # Be sure to include the domain name here because Common Name is not so commonly honoured by itself
DNS.2 = bar.$NAME # Optionally, add additional domains (I've added a subdomain here)
IP.1 = 192.168.0.13 # Optionally, add an IP address (if the connection which you have planned requires it)
EOF
# Create the signed certificate
openssl x509 -req -in $NAME.csr -CA myCA.pem -CAkey myCA.key -CAcreateserial \
-out $NAME.crt -days 825 -sha256 -extfile $NAME.ext

Windows系统转换证书代码

1
openssl pkcs12 -export -out myCA.pfx -inkey myCA.key -in myCA.pem

最佳实践

  • 使用 mkcert 工具:mkcert 是一个跨平台的工具,可以轻松生成本地受信任的开发证书。安装并使用它:
1
2
mkcert -install
mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1
  • 配置 subjectAltName:从Chrome 58开始,证书必须使用 subjectAltName 来标识其主机。确保在生成证书时包含正确的 subjectAltName 配置。

常见问题

证书导入后仍然提示错误

  • 检查证书的 subjectAltName 是否包含正确的域名或IP地址。
  • 确保将根证书颁发机构的证书导入到受信任的根证书颁发机构存储中。
  • 尝试重启Chrome浏览器,确保所有窗口都已关闭。

Chrome版本不同导致方法不适用

不同版本的Chrome可能会有不同的处理方式。例如,Chrome 119及以上版本需要启用 chrome://flags/#temporary-unexpire-flags-m118,而Chrome 118及以下版本需要启用 chrome://flags/#allow-insecure-localhost。根据自己的Chrome版本选择合适的方法。

输入绕过短语无效

如果输入 thisisunsafe 等绕过短语无效,可能是Chrome版本不支持该短语,或者存在其他安全限制。可以尝试其他方法,如启用相关标志或使用工具生成受信任的证书。


让Chrome接受自签名的本地主机证书
https://119291.xyz/posts/getting-chrome-to-accept-self-signed-localhost-certificate/
作者
ww
发布于
2025年5月26日
许可协议