|
| 1 | +# 证书管理 |
| 2 | + |
| 3 | +在使用设备编号、设备密钥、连接用户名认证的基础上,平台还可以使用设备证书进行认证(需将相应设备的认证方式设置为证书认证),进一步保障通信安全性。 |
| 4 | + |
| 5 | +**ActorCloud** 提供设备证书生成功能,支持证书与账户下任意设备绑定并配置其可用性。 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## 证书创建 |
| 11 | + |
| 12 | +- 输入证书名称,选择证书可用性后创建证书。创建后请立即下载并妥善保管相关证书。 |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## 绑定设备 |
| 19 | + |
| 20 | +- 设备使用 SSL/TLS 安全连接时应当使用已绑定证书进行加密通信,在证书详情页可以修改证书信息,管理绑定设备。 |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## 单向认证 |
| 27 | + |
| 28 | +使用 8883 端口进行 SSL/TLS 加密通信: |
| 29 | + |
| 30 | +- 设备使用设备编号、设备密钥、连接用户名认证 |
| 31 | +- 设备认证方式为 Token |
| 32 | +- 设备接入地址为:`iot.acotecloud.io:8883` |
| 33 | + |
| 34 | +使用设备编号,设备密钥,连接用户名可以连接到消息服务器: |
| 35 | + |
| 36 | +- 设备编号:设备连接 MQTT 服务器时,client_id 即为设备编号; |
| 37 | +- 设备密钥:对应设备连接时 MQTT 连接密码; |
| 38 | +- 连接用户名:对应设备连接时 MQTT 连接用户名。 |
| 39 | + |
| 40 | +设备详情页编码信息标签页可以查看设备认证信息: |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +示例接入代码: |
| 45 | +```python |
| 46 | +# --coding: utf-8-- |
| 47 | + |
| 48 | +import ssl |
| 49 | +import paho.mqtt.client as mqtt |
| 50 | +import json |
| 51 | + |
| 52 | +# 设备需要在 ActorCloud 平台注册 |
| 53 | +client_id = 'ef14be2cfd2f555397bde858750cbda8' |
| 54 | +username = 'ef14be2cfd2f555397bde858750cbda8' |
| 55 | +password = '53e9b7036fcb5f178d30958a563ba049' |
| 56 | +HOST = 'iot.actorcloud.io' |
| 57 | +PORT = 8883 |
| 58 | + |
| 59 | + |
| 60 | +def on_connect(client, userdata, flags, rc): |
| 61 | + print('Connected with result code ' + str(rc)) |
| 62 | + client.subscribe('/hello') |
| 63 | + client.publish('/hello', json.dumps({ |
| 64 | + 'hello': 'world' |
| 65 | + })) |
| 66 | + |
| 67 | + |
| 68 | +# 设备控制、分组控制及其他方式 publish 的消息统一在此处理 |
| 69 | +def on_message(client, userdata, msg): |
| 70 | + print('topic: {0}, payload: {1}'.format(msg.topic, str(msg.payload))) |
| 71 | + # 其他处理逻辑 |
| 72 | + pass |
| 73 | + |
| 74 | +client = mqtt.Client(client_id=client_id) |
| 75 | + |
| 76 | +# 单向认证仍需要用户名密码 |
| 77 | +client.username_pw_set(username, password) |
| 78 | + |
| 79 | +# 设置证书信息 |
| 80 | +client.tls_set(certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, ciphers=None) |
| 81 | +client.on_connect = on_connect |
| 82 | +client.on_message = on_message |
| 83 | + |
| 84 | +client.connect(HOST, PORT) |
| 85 | + |
| 86 | +client.loop_forever() |
| 87 | + |
| 88 | +``` |
| 89 | + |
| 90 | +如果您使用 MQTT fx 测试,连接设置如下: |
| 91 | + |
| 92 | +- 按照**设备安全**页面中提供的信息设置基础接入信息,使用 8883 端口,指定证书 SSL/TLS 加密方式 |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +## 双向认证 |
| 102 | + |
| 103 | +使用 8884 端口进行 SSL/TLS 通信,服务器设备使用绑定的证书验证设备身份: |
| 104 | + |
| 105 | +- 需要使用**设备编号**与**连接用户名** |
| 106 | +- 设备认证方式为 证书 |
| 107 | +- 使用平台绑定设备的证书 |
| 108 | +- 设备接入地址为:`iot.acotecloud.io:8884` |
| 109 | + |
| 110 | +设备绑定相应证书详情页中,点击下载证书后解压文件得到: |
| 111 | + |
| 112 | +```bash |
| 113 | +common_certs/ |
| 114 | +├── common.crt # 自签名证书; |
| 115 | +├── common.key # 自签名证书密钥 |
| 116 | +└── root_ca.crt # ActorCloud 根证书; |
| 117 | +``` |
| 118 | + |
| 119 | +使用双向认证接入的设备无需密码验证,但设备需绑定并使用其匹配证书才能认证成功。 |
| 120 | + |
| 121 | + |
| 122 | +示例代码: |
| 123 | +```python |
| 124 | +# --coding: utf-8-- |
| 125 | + |
| 126 | +import ssl |
| 127 | +import paho.mqtt.client as mqtt |
| 128 | +import json |
| 129 | + |
| 130 | +# 设备需要在 ActorCloud 平台注册且认证方式为 "证书" |
| 131 | +client_id = 'ef14be2cfd2f555397bde858750cbda8' |
| 132 | +username = 'ef14be2cfd2f555397bde858750cbda8' |
| 133 | +password = '630a7f6b54d75e50a2e59b4baca722d4' |
| 134 | +HOST = 'iot.actorcloud.io' |
| 135 | + |
| 136 | +# 双向认证端口 |
| 137 | +PORT = 8884 |
| 138 | + |
| 139 | + |
| 140 | +def on_connect(client, userdata, flags, rc): |
| 141 | + print('Connected with result code ' + str(rc)) |
| 142 | + client.subscribe('/hello') |
| 143 | + client.publish('/hello', json.dumps({ |
| 144 | + 'hello': 'world' |
| 145 | + })) |
| 146 | + |
| 147 | + |
| 148 | +# 设备控制、分组控制及其他方式 publish 的消息统一在此处理 |
| 149 | +def on_message(client, userdata, msg): |
| 150 | + print('topic: {0}, payload: {1}'.format(msg.topic, str(msg.payload))) |
| 151 | + # 其他处理逻辑 |
| 152 | + pass |
| 153 | + |
| 154 | +client = mqtt.Client(client_id=client_id) |
| 155 | +# 双向认证只需要使用用户名 |
| 156 | +client.username_pw_set(username) |
| 157 | +# 设置 PROT 为 8884 并设置根证书、签名证书及密钥路径 |
| 158 | + |
| 159 | +client.tls_set(ca_certs='./common_certs/root_ca.crt', |
| 160 | + certfile='./common_certs/common.crt', |
| 161 | + keyfile='./common_certs/common.key', |
| 162 | + tls_version=ssl.PROTOCOL_TLSv1_2) |
| 163 | + |
| 164 | +client.tls_insecure_set(True) |
| 165 | + |
| 166 | +client.on_connect = on_connect |
| 167 | +client.on_message = on_message |
| 168 | + |
| 169 | +client.connect(HOST, PORT) |
| 170 | + |
| 171 | +client.loop_forever() |
| 172 | +``` |
| 173 | + |
| 174 | + |
| 175 | +如果您使用 MQTT fx 测试,连接设置如下: |
| 176 | + |
| 177 | +- 按照**设备安全**页面中提供的信息设置基础接入信息,需要指定 Client ID 与 User Name,同时使用 8884 端口,指定证书 SSL/TLS 加密方式并设置自签名证书路径: |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments