Mqtt使用tls证书

  1. 1. 概述
  2. 2. 示例

Mqtt使用tls证书

1. 概述

MQTT是物联网常用协议,在客户端和服务端进行交互时,通常使用MQTTS(TLS)对数据进行加密。

2. 示例

    // Read in the cert file
    rootCrt, err := ioutil.ReadFile(ca)
    if err != nil {
        logrus.WithError(err).Fatalln("root crt file read error")
    }
    rootCAs := x509.NewCertPool()
    // Append our cert to the system pool
    if ok := rootCAs.AppendCertsFromPEM(rootCrt); !ok {
        logrus.Fatalln("root crt append error")
    }
    clientCA, err := tls.LoadX509KeyPair(crt, key)
    if err != nil {
        logrus.WithError(err).Fatalln("client ca file read error")
    }
    // TLS
    t = &tls.Config{
        InsecureSkipVerify: true,
        RootCAs:            rootCAs,
        Certificates: []tls.Certificate{
            clientCA,
        },
    }
    opts.SetTLSConfig(t)

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

💰

×

Help us with donation