本文主要是介绍NodeRed base64 编码的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
起因:
EMQ X 的 HTTP API 使用 Basic 认证 (opens new window) 方式,EMQ X 的 HTTP API 使用 Basic 认证 (opens new window) 方式,id
和 password
须分别填写 AppID 和 AppSecret。 默认的 AppID 和 AppSecret 是:admin/public
。你可以在 Dashboard 的左侧菜单栏里,选择 "管理" -> "应用" 来修改和添加 AppID/AppSecret。
通过链接转到WIKI
基本访问认证
在HTTP事务的上下文中,基本访问身份验证是HTTP 用户代理(例如Web 浏览器)在发出请求时提供用户名和密码的一种方法。在基本的 HTTP 身份验证中,请求包含形式为 的标头字段Authorization: Basic <credentials>
,其中凭据是ID 和密码的Base64编码,由单个冒号连接:
。
它在2015年的RFC 7617 中指定,它从 1999 年起废弃了RFC 2617
上述这段话最关键的是“其中凭据是ID 和密码的Base64编码,由单个冒号连接:”。在进行Base64编码前数据应该按如下形式准备:admin:public
试验:POSTMAN连接试验:通过HTTP GET请求获取Broker 基本信息
EMQ 详细说明
GET /api/v4/brokers/{node}
点击Send按钮后获取到EMQ X 服务器返回的消息:
表明试验成功。但不能止步于此,需进一步探究GET请求发送了怎样的数据。还是利用POSTMAN。
具体HTTP报文如下:
下划红线文字就是Authorization: Basic <credentials>格式对应的具体数据,现在
“YWRtaW46cHVibGlj”就是admin:public经过Base64编码后的结果。
NodeRed验证:
在NodeRed中利用节点管理安装Base64节点,具体如图:
一般安装完我自己也不会用,得看例子学习下如何使用,用必应搜索“nodered base64”,得到如图结果:
点击Base64链接
进入后把Sample Flow的代码导入到NodeRed中,
得到如下流图:
函数节点具体内容如图:注意划红线的字符串(我修改了NodeRed官网的例程)与文章开头提到的数据准备是一样的。
部署后运行得到结果:
与POSTMAN的 报文结果是一样的:
至此关于HTTP基本访问认证Base64编码问题解决。
具体源码:
[{"id": "688fc49188120316","type": "tab","label": "流程 5","disabled": false,"info": "","env": []},{"id": "d2ccae00.2d335","type": "inject","z": "688fc49188120316","name": "","props": [{"p": "payload","v": "","vt": "str"},{"p": "topic","v": "","vt": "str"}],"repeat": "","crontab": "","once": false,"topic": "","payload": "","payloadType": "str","x": 230,"y": 360,"wires": [["e03cae10.1fc35"]]},{"id": "b778ef09.48871","type": "base64","z": "688fc49188120316","name": "","action": "","property": "payload","x": 525.5,"y": 361,"wires": [["6295d1b1.9d6a3","46b597ba.b94a68"]]},{"id": "6295d1b1.9d6a3","type": "debug","z": "688fc49188120316","name": "","active": true,"tosidebar": true,"console": false,"tostatus": false,"complete": "payload","targetType": "msg","statusVal": "","statusType": "auto","x": 724,"y": 361,"wires": []},{"id": "ead9e7c9.152618","type": "debug","z": "688fc49188120316","name": "","active": true,"tosidebar": true,"console": false,"tostatus": false,"complete": "payload","targetType": "msg","statusVal": "","statusType": "auto","x": 724,"y": 441,"wires": []},{"id": "46b597ba.b94a68","type": "base64","z": "688fc49188120316","name": "","action": "","property": "payload","x": 525.5,"y": 441,"wires": [["ead9e7c9.152618"]]},{"id": "1c9124e9.e36edb","type": "inject","z": "688fc49188120316","name": "","repeat": "","crontab": "","once": false,"topic": "","payload": "","payloadType": "date","x": 1889,"y": 314,"wires": [[]]},{"id": "48a892ea.b7576c","type": "debug","z": "688fc49188120316","name": "","active": true,"console": "false","complete": "false","x": 2285,"y": 411,"wires": []},{"id": "e03cae10.1fc35","type": "function","z": "688fc49188120316","name": "","func": "msg.payload = new Buffer.from(\"admin:public\");\nreturn msg;","outputs": 1,"noerr": 0,"initialize": "","finalize": "","libs": [],"x": 364,"y": 361,"wires": [["b778ef09.48871"]]}
]
这篇关于NodeRed base64 编码的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!