我想知道如何为所有用户设置通用规则? 我希望让所有用户都可以订阅以/c2d/xxx开头的主题并发布以/d2c/xxx开头的主题 例如:/c2d/config/get/0123456789A,/c2d/dashboard/info/0123456789A
这是我的第一个版本的 acl,
# Give admin full access to everything
user admin
topic readwrite #
# Allow all users to read and write to specific topics
topic readwrite all users /c2d/#
topic readwrite all users /d2c/#
除管理员之外的用户无法接收以/c2d开头的主题,例如:/c2d/config/get/0123456789A 阅读文档后,我像这样编辑我的 acl 文件:
# Give admin full access to everything
user admin
topic readwrite #
# Allow all users to read and write to specific topics
#topic readwrite all users /c2d/#
#topic readwrite all users /d2c/#
pattern readwrite /c2d/#
pattern readwrite /d2c/#
但是我重新加载配置后(dockerkill --signal=HUP mqtt-container)我仍然无法接收其他用户以/c2d/xxx开头的主题,请帮助QQ
这是我的python代码,用于订阅主题消息,只有当用户是管理员时才能接收消息,如接收主题:/c2d/xxx
def on_message(client, userdata, msg):
payload_string = msg.payload.decode("utf-8")
payload_json = json.loads(payload_string)
topic = msg.topic
print(f"receive topic: {topic}")
try:
if topic == f'/c2d/info/{MAC}':
payload = get_c2d_info_payload()
client.publish(f"/d2c/info/{MAC}", json.dumps(payload))
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
except Exception as e:
print(e)
if __name__ == '__main__':
try:
client = mqtt.Client()
client.on_connect = on_connect
client.username_pw_set(username='user2', password='xxxx')
client.tls_set(ca_certs="./mqtt_ssl/ca.crt",
certfile="./mqtt_ssl/client.crt",
keyfile="./mqtt_ssl/client.key",
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLS)
client.tls_insecure_set(True)
client.on_message = on_message
client.connect(MQTT_HOST, MQTT_PORT, 60)
print(f'EW50 {MAC} simulator started successfully.')
# return_code是為了抓取loop被正常結束
return_code = client.loop_forever()
来自文档
第一组主题应用于匿名客户端,假设 允许匿名是真的。用户特定主题 ACL 在 用户线路如下:
user <username>
这意味着将
user <username>
行应用于该用户之后的任何内容。 ACL 文件中的顺序很重要。
在为特定用户定义规则之前,您还应该考虑使用
pattern
创建规则。
但是对于修复,您尝试使用身份验证/授权插件可能是更好的选择,然后您可以通过编程方式生成 ACL 规则。