为linux生成并使用证书

为linux生成并使用证书

生成证书

  1. 执行命令 openssl genrsa –out private.key 2048

    Generating RSA private key, 2048 bit long modulus (2 primes)
    ................................+++++
    .......................................................+++++
    
  2. 执行命令 openssl req -new -key private.key -out csr.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]:CH
    State or Province Name (full name) [Some-State]:Shanghai
    Locality Name (eg, city) []:abc
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:abc
    Organizational Unit Name (eg, section) []:abc
    Common Name (e.g. server FQDN or YOUR name) []:abc
    Email Address []:abc@abc.com
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:abc
    
  3. 执行命令 openssl x509 -req -days 365 -in csr.csr -signkey private.key -out certificate.crt

    Signature ok
    subject=C = CH, ST = Shanghai, L = abc, O = abc, OU = abc, CN = abc, emailAddress = abc#abc.com
    Getting Private key
    

使用证书,以go语言为例

  1. 编写代码
    // main.go
    import (
    	"github.com/gogf/gf/frame/g"
    )
    
    func main() {
    	g.Server().BindHandler("GET:/GetCurrentTime", HandleGetCurrentTime)	
    	g.Server().EnableHTTPS("certificate.crt", "private.key")	
    	g.Server().Run()
    }
    
  2. 编译并执行 go run main.go
    2023-12-04T17:27:43+08:00 warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
    
      SERVER  | DOMAIN  | ADDRESS | METHOD |       ROUTE        |           HANDLER            | MIDDLEWARE  
    ----------|---------|---------|--------|--------------------|------------------------------|-------------
      default | default | tls:443 | GET    | /GetCurrentTime    | main.HandleGetCurrentTime    |             
    ----------|---------|---------|--------|--------------------|------------------------------|-------------
     
    2023-12-04 17:27:46.309 14752: https server started listening on [:443] 
    
  3. 没有设置端口,启动端口默认展示443则认为证书配置成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值