https协议需要使用ssl证书,所以借助OpenSsl自签应该ssl证书


文章目录

  • 方法
  • 创建ssl证书
  • 1、创建密钥
  • 2、生成CSR(证书签名请求)
  • 3、删除密钥中的密码
  • 4、生成自签名证书
  • 5、生成pem格式的公钥
  • 示例



方法

http在公网传输的时候,都是明文的,因此考虑加个ssl证书。


创建ssl证书

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

1、创建密钥

使用openssl工具生成一个RSA私钥

openssl genrsa -des3 -out server.key 2048

注意:生成私钥,需要提供一个至少4位,最多1023位的密码。

2、生成CSR(证书签名请求)

openssl req -new -key server.key -out server.csr

说明:需要依次输入国家,地区,城市,组织,组织单位,Common Name和Email。其中Common Name,可以写自己的名字或者域名,如果要支持https,Common Name应该与域名保持一致,否则会引起浏览器警告。
可以将证书发送给证书颁发机构(CA),CA验证过请求者的身份之后,会出具签名证书,需要花钱。另外,如果只是内部或者测试需求,也可以使用OpenSSL实现自签名。
创建证书会提示输入以下数据(翻译):

You are about to be asked to enter information that will be incorporated #您将被要求输入将被合并的信息 into your certificate request. #你的证书申请。 What you are about to enter is what is called a Distinguished Name or a DN. #您将要输入的内容称为可分辨名称或DN。 There are quite a few fields but you can leave some blank #有相当多的字段,但你可以留下一些空白 For some fields there will be a default value, If you enter '.', the field will be left blank. #对于某些字段,会有一个默认值,如果输入“.”,该字段将留空。 ----- Country Name (2 letter code) [AU]: #国家名称(两个字母代码)[AU]: State or Province Name (full name) [Some-State]: #州或省名称(全名)[Some-State]: Locality Name (eg, city) []: #地名(如城市)[]: Organization Name (eg, company) [Internet Widgits Pty Ltd]: #组织名称(如公司)[Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: #组织单位名称(如部门)[]: Common Name (e.g. server FQDN or YOUR name) []: #组织单位名称(如部门)[]: Email Address []: #电子邮件地址[]: A challenge password []: #质询密码[]: An optional company name []: #可选公司名称[]:

3、删除密钥中的密码

openssl rsa -in server.key -out server.key

说明:如果不删除密码,在应用加载的时候会出现输入密码进行验证的情况,不方便自动化部署。

4、生成自签名证书

内部或者测试使用,只要忽略证书提醒就可以了。

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

5、生成pem格式的公钥

有些服务,需要有pem格式的证书才能正常加载,可以用下面的命令:

openssl x509 -in server.crt -out server.pem -outform PEM

示例

$ openssl genrsa -out privatekey.pem 1024
> Generating RSA private key, 1024 bit long modulus (2 primes)
> .+++++
> ...................+++++
> e is 65537 (0x010001)
$ openssl req -new -key privatekey.pem -out certrequest.csr
> You are about to be asked to enter information that will be incorporated
> into your certificate request.
> What you are about to enter is what is called a Distinguished Name or a DN.
> There are quite a few fields but you can leave some blank
> For some fields there will be a default value,
> If you enter '.', the field will be left blank.
> -----
> Country Name (2 letter code) [AU]:CN
> State or Province Name (full name) [Some-State]:China
> Locality Name (eg, city) []:SmallYou
> Organization Name (eg, company) [Internet Widgits Pty Ltd]:Superhero Ltd.
> Organizational Unit Name (eg, section) []:You
> Common Name (e.g. server FQDN or YOUR name) []:192.168.0.105:8080
> Email Address []:email@qq.com
> 
> Please enter the following 'extra' attributes
> to be sent with your certificate request
> A challenge password []:
> An optional company name []:
$ openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem
> Signature ok
> subject=C = CN, ST = China, L = SmallYou, O = Superhero Ltd., OU = You, CN = 192.168.0.105:8080, emailAddress = email@qq.com
> Getting Private key
$ ls
>     Directory: Z:\app\cert
> Mode                 LastWriteTime         Length Name
> ----                 -------------         ------ ----
> -a---           2021/2/18    10:45           1022 certificate.pem
> -a---           2021/2/18    10:45            732 certrequest.csr
> -a---           2021/2/18    10:42            902 privatekey.pem

虽然浏览器会提示证书风险,但是已经承认你的网站已经是HTTPS了。