创建本地可用的ssl证书
背景
修改hosts后发现app请求的api都是https的,需要解决ssl证书问题。
解决
根证书(客户端用)
openssl genrsa -des3 -out myRootCA.key 2048 openssl req -x509 -new -nodes -key myRootCA.key -sha256 -days 1024 -out myRootCA.pem # 生成的pem证书要安装到客户端设备,如电脑、安卓手机
服务端证书
创建文件
server.cnf
[req] default_bits = 2048 prompt = no default_md = sha256 distinguished_name = dn [dn] C=CN ST=libra L=libra O=libra OU=My Local Server emailAddress=example@domain.com # 这里换成自己hosts的域名 CN = localhost
执行命令
openssl req -new -sha256 -nodes -out server.csr -newkey rsa:2048 -keyout server.key -config <(cat server.cnf) # windows可以换成以下命令执行 # openssl req -new -sha256 -nodes -out server.csr -newkey rsa:2048 -keyout server.key -config server.cnf
创建文件
v3.ext
authorityKeyIdentifier=keyid,issuer basicConstraints=CA:FALSE keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment subjectAltName = @alt_names [alt_names] # 这里换成自己hosts的域名 DNS.1 = localhost
执行命令
openssl x509 -req -in server.csr -CA myRootCA.pem -CAkey myRootCA.key -CAcreateserial -out server.crt -days 500 -sha256 -extfile v3.ext
部署服务端证书
根据自己的服务器环境部署ssl证书文件即可
备注
- 因为是本地证书,所以需要在服务器和客户端中安装证书
- 只有对应的客户端安装了证书才不会提示“不安全连接”
创建本地可用的ssl证书
http://blog.icy8.cn/posts/36547/