grpc TLS证书认证
发布于 2022-10-16 16:10:18阅读 844
首先,申请证书,这个证书和nginx用的是一样的,具体步骤请移步:RabbitMQ插件之MQTT
ca.cer
www.cuiwei.net.key
www.cuiwei.net.pem
看代码
服务端
...
// 从输入证书文件和密钥文件为服务端构造TLS凭证
creds, err := credentials.NewServerTLSFromFile("./www.cuiwei.net.pem", "./www.cuiwei.net.key")
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
s := grpc.NewServer(grpc.Creds(creds))
...
客户端
...
//从输入的证书文件中为客户端构造TLS凭证
creds, err := credentials.NewClientTLSFromFile("./ca.cer", "www.cuiwei.net")
if err != nil {
log.Fatalf("Failed to create TLS credentials %v", err)
}
conn, err := grpc.Dial(":50051", grpc.WithTransportCredentials(creds))
...