簡體   English   中英

使用 mqtt 用戶名和密碼連接到 aws iot core

[英]Connecting to aws iot core with mqtt username and password

aws 教程中的示例未顯示使用用戶名和密碼通過 mqtt 登錄的方法。 如何使用用戶名和密碼連接自定義身份驗證?

我嘗試使用自定義身份驗證,但沒有用。

aws 物聯網核心文檔

  • 我按照 aws 文檔中的步驟操作。 我編輯了一些使用 MQTT 用戶名和密碼登錄的部分。 鏈接: https://docs.aws.amazon.com/iot/latest/developerguide/config-custom-auth.html 在此處輸入圖像描述

  • 這是我的授權人在此處輸入圖像描述

  • 這是我的 Lambda Function(arn 地址是正確的)

     // A simple Lambda function for an authorizer. exports.handler = function(event, context, callback) { var uname = event.protocolData.mqtt.username; var pwd = event.protocolData.mqtt.password; var buff = new Buffer(pwd, 'base64'); var passwd = buff.toString('ascii'); switch (passwd) { case 'test': callback(null, generateAuthResponse(passwd, 'Allow')); default: callback(null, generateAuthResponse(passwd, 'Deny')); } }; // Helper function to generate the authorization response. var generateAuthResponse = function(token, effect) { var authResponse = {}; authResponse.isAuthenticated = true; authResponse.principalId = 'TEST123'; var policyDocument = {}; policyDocument.Version = '2012-10-17'; policyDocument.Statement = []; var publishStatement = {}; var connectStatement = {}; connectStatement.Action = ["iot:Connect"]; connectStatement.Effect = effect; connectStatement.Resource = ["arn:aws:iot:eu-west-1:<myarn>:client/myClientName"]; publishStatement.Action = ["iot:Publish"]; publishStatement.Effect = effect; publishStatement.Resource = ["arn:aws:iot:eu-west-1:<myarn>:topic/telemetry/myClientName"]; policyDocument.Statement[0] = connectStatement; policyDocument.Statement[1] = publishStatement; authResponse.policyDocuments = [policyDocument]; authResponse.disconnectAfterInSeconds = 3600; authResponse.refreshAfterInSeconds = 300; return authResponse; }

  • 當我使用 aws cli 測試它時,一切似乎都很好。 在此處輸入圖像描述

  • 我正在使用 node-red 進行測試。 但我無法連接。 在此處輸入圖像描述

  • 當我嘗試使用 mosquitto 時,我也無法連接。 在此處輸入圖像描述

AWS IoT Core 確實希望您使用客戶端證書連接到 MQTT 代理。

或者,您可以讓您的服務器使用您的訪問密鑰 ID 和授權用戶的秘密訪問密鑰生成簽名版本 4 url 我不喜歡這種方式,但它在緊要關頭有效。 請參閱https://glitch.com/edit/#?/itp-arduino-workshop.path=AWS-IoT-ws-url.js了解一種執行此操作的方法。

更好的方法是設置 Cognito 以允許訪問 IoT Core。 AWS Innovator Island 教程演示了如何執行此操作。 有關詳細信息,請參閱第 2 部分的后端部分

我讓它與以下客戶端一起使用示例授權代碼 密碼不需要編碼。

mosquitto_sub \
--cafile AmazonRootCA1.pem \
-h <your-endpoint>-ats.iot.<region>.amazonaws.com \
-p 443 \
-u 'test?x-amz-customauthorizer-name=AuthorizerName' \
-P 'test' \
-t 'notification' \
-i browser \
--tls-alpn mqtt \
-d
Client browser sending CONNECT
Client browser received CONNACK (0)
Client browser sending SUBSCRIBE (Mid: 1, Topic: notification, QoS: 0, Options: 0x00)
Client browser received SUBACK
Subscribed (mid: 1): 0

您在日志中看到任何錯誤嗎?

  • aws logs tail --follow AWSIotLogsV2
  • aws logs tail --follow /aws/lambda/AuthorizerFunctionName

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM