{"id":11524,"date":"2023-02-01T16:36:45","date_gmt":"2023-02-01T08:36:45","guid":{"rendered":"https:\/\/www.modbus.cn\/?p=11524"},"modified":"2023-02-01T16:36:48","modified_gmt":"2023-02-01T08:36:48","slug":"node-red-zhong-you-guan-yong-hu-deng-lu-jian-quan-de-yan-jiu","status":"publish","type":"post","link":"https:\/\/www.modbus.cn\/en\/11524.html","title":{"rendered":"node-red\u4e2d\u6709\u5173\u7528\u6237\u767b\u5f55\u9274\u6743\u7684\u7814\u7a76"},"content":{"rendered":"<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u5730\uff0cnode-red\u7f16\u8f91\u5668\u53ef\u4ee5\u88ab\u4efb\u4f55\u8bbf\u95ee\u7684\u7528\u6237\u64cd\u4f5c\uff0c\u5305\u62ec\u4fee\u6539\u8282\u70b9\uff0c\u6d41\u6570\u636e\uff0c\u91cd\u65b0\u90e8\u7f72\u6d41\u3002<br>\u8fd9\u79cd\u9ed8\u8ba4\u7684\u90e8\u7f72\u65b9\u5f0f\u53ea\u9002\u7528\u4e8e\u8fd0\u884c\u5728\u53ef\u9760\u7684\u7f51\u7edc\u4e2d\u3002\u4e0b\u9762\u6211\u5c31\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5728\u516c\u7f51\u4e0a\u90e8\u7f72node-red\u540e\uff0c\u5982\u4f55\u5bf9\u5176\u8fdb\u884c\u5b89\u5168\u52a0\u56fa\u548c\u6743\u9650\u9a8c\u8bc1\u3002<br>\u4e3b\u8981\u5206\u4e3a\u4e09\u90e8\u5206<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f00\u542fhttps\u6743\u9650<\/li>\n\n\n\n<li>\u4fdd\u62a4\u7f16\u8f91\u5668\u548cadmin api<\/li>\n\n\n\n<li>\u4fdd\u62a4http\u8282\u70b9\u548cnode-red\u7684\u4eea\u8868\u76d8<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u5f00\u542fhttps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">node-red\u9ed8\u8ba4\u662f\u4f7f\u7528http\u6765\u8fdb\u884c\u8bbf\u95ee\uff0c\u5982\u679c\u8981\u914d\u7f6ehttps\u8bbf\u95ee\uff0c\u4f60\u9700\u8981\u5728node-red\u7684\u914d\u7f6e\u6587\u4ef6<code>setting<\/code>\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e00\u4e0b<code>https<\/code>\u90e8\u5206\u7684\u5185\u5bb9<br>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d https\u914d\u7f6e\u9879\u53ef\u4ee5\u662f\u4e00\u4e2ajson\u9759\u6001\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u5b8c\u6574\u7684\u914d\u7f6e\u53c2\u6570\u4e8bnodejs\u4e2dhttp\u6a21\u5757\u7684\u914d\u7f6e&nbsp;<a href=\"https:\/\/nodejs.org\/api\/tls.html#tls_tls_createsecurecontext_options\" rel=\"noopener noreferrer nofollow\">\u70b9\u51fb\u94fe\u63a5\u53ef\u4ee5\u67e5\u770b\u5230\u5b8c\u6574\u7684\u914d\u7f6e<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8be5\u914d\u7f6e\u9879\u4e2d\uff0c\u81f3\u5c11\u8981\u6709\u4e8c\u9879\u9700\u8981\u8fd4\u56de\u6216\u914d\u7f6e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>key PEM\u683c\u5f0f\u5316\u540e\u7684\u5bc6\u94a5\uff0c\u7c7b\u578b\u53ef\u4ee5\u662fString \u6216Buffer<\/li>\n\n\n\n<li>cert PEM\u683c\u5f0f\u5316\u540e\u7684 Cert chain\uff0c\u7c7b\u578b\u53ef\u4ee5\u662fString \u6216Buffer<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u7f6e\u7684\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https: {\n    key: require(\"fs\").readFileSync('privkey.pem'),\n    cert: require(\"fs\").readFileSync('cert.pem')\n},\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u8981\u914d\u7f6e\u6210\u4e00\u4e2afunction\u7684\u8bdd\uff0c\u8bf7\u8bb0\u5f97\u5c06key\u4e0ecert\u653e\u5230promise\u4e2d\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>https: function() {\n    return new Promise((resolve, reject) =&gt; {\n        var key, cert;\n        <em>\/\/ Do some work to obtain valid certificates<\/em>\n        <em>\/\/ ...<\/em>\n        resolve({\n            key: key\n            cert: cert\n        })\n    });\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u8981\u5728\u914d\u7f6ehttps\u540e\uff0c\u81ea\u52a8\u5237\u65b0\u8bc1\u4e66\uff0c\u5e76\u4e14\u4e0d\u7528\u91cd\u542fnode-red\uff0cnode-red\u4e5f\u662f\u80fd\u505a\u5230\u7684\uff0c\u9700\u8981nodejs\u572811\u4e4b\u4e0a\uff0c\u5e76\u4e14https\u5fc5\u987b\u914d\u7f6e\u6210function\uff0c\u63a5\u7740\u8bbe\u7f6e&nbsp;<code>httpsRefreshInterval<\/code>\uff0c\u7c7b\u578b\u4e3a\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u51e0\u5c0f\u65f6\u5185\u5faa\u73af\u81ea\u52a8\u5237\u65b0\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u7f16\u8f91\u5668\u4e0eAdmin API\u7684\u9274\u6743<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">node-red\u7684\u7f16\u8f91\u5668\u4e0eAdmin api\u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u9274\u6743<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u7528\u6237\u540d \u5bc6\u7801\u8bc1\u4e66\u6765\u8fdb\u884c\u9274\u6743<\/li>\n\n\n\n<li>\u4f7f\u7528\u4efb\u4f55OAuth\/OpenID\u63d0\u4f9b\u5546\u6765\u8fdb\u884c\u9274\u6743\uff0c\u5982Twitter\u6d3bGitHub\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u57fa\u4e8e\u7528\u6237\u540d\u548c\u5bc6\u7801\u7684\u9274\u6743\u53ea\u9700\u8981\u5728setting\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u5373\u53ef<br>\u5982\u4e0b\u662f\u4e00\u4e2a\u914d\u7f6e\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>adminAuth: {\n    type: \"credentials\",\n    users: &#91;\n        {\n            username: \"admin\",\n            password: \"$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.\",\n            permissions: \"*\"\n        },\n        {\n            username: \"george\",\n            password: \"$2b$08$wuAqPiKJlVN27eF5qJp.RuQYuy6ZYONW7a\/UWYxDTtwKFCdB8F19y\",\n            permissions: \"read\"\n        }\n    ]\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ba1\u7406\u5458\u53ef\u4ee5\u914d\u7f6e\u591a\u4e2a\u7528\u6237\u3002\u8fd9\u4e9b\u7528\u6237\u7684\u767b\u5f55\u53ca\u6743\u9650\u6570\u636e\u90fd\u662f\u5199\u6b7b\u7684\u3002\u5305\u62ec\u767b\u5f55\u7684\u8d26\u53f7\uff0c\u5bc6\u7801\uff0c\u548c\u6743\u9650\u3002\u5176\u4e2d\u5bc6\u7801\u662f\u4f7f\u7528bcrypt \u7b97\u6cd5\u5de5\u5177\u8fdb\u884chash\u52a0\u5bc6\u3002<br>\u751f\u6210hash\u5bc6\u7801\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>node-red admin hash-pw<\/code>(\u5728node-red 1.1.0 \u4e4b\u540e)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528OAuth\/OpenID\u6765\u8fdb\u884c\u6388\u6743<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dminAuth: {\n    type:\"strategy\",\n    strategy: {\n        name: \"twitter\",\n        label: 'Sign in with Twitter',\n        icon:\"fa-twitter\",\n        strategy: require(\"passport-twitter\").Strategy,\n        options: {\n            consumerKey: TWITTER_APP_CONSUMER_KEY,\n            consumerSecret: TWITTER_APP_CONSUMER_SECRET,\n            callbackURL: \"http:\/\/example.com\/auth\/strategy\/callback\",\n            verify: function(token, tokenSecret, profile, done) {\n                done(null, profile);\n            }\n        },\n    },\n    users: &#91;\n       { username: \"knolleary\",permissions: &#91;\"*\"]}\n    ]\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u7f6e\u9879\u8be6\u89e3\uff1a<br>name\uff1a \u7b56\u7565\u540d\u79f0<br>lable\/icon\uff1a\u5728\u767b\u5f55\u9875\u663e\u793a\u7684\u4fe1\u606f<br>strategy\uff1a\u9700\u8981\u4f7f\u7528\u5230\u7684\u5e93<br>options\uff1a\u6839\u636estrategy\u6765\u8fdb\u884c\u914d\u7f6e\uff0c\u9700\u8981\u4f7f\u7528\u7684\u53c2\u6570<br>verify\uff1a\u9a8c\u8bc1\u51fd\u6570\uff0c\u6700\u540e\u4f7f\u7528done\u51fd\u6570\u5c06\u7528\u6237\u8d44\u6599\u5411\u4e0b\u4f20\u9012<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u8bbe\u7f6e\u9ed8\u8ba4\u7528\u6237<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u8981\u9650\u5b9a\u6ca1\u6709\u767b\u5f55\u65f6\uff0cnode-red\u7684\u4e00\u4e9b\u6743\u9650\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bbe\u7f6e\u9ed8\u8ba4\u7528\u6237\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0<br>\u914d\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u7528\u6237\uff0c\u5e76\u914d\u7f6e\u5176\u5bf9\u4e8e\u7684\u6743\u9650\u3002\u6743\u9650\u53ef\u4ee5\u662f\u5168\u90e8\uff0c\u4e5f\u53ef\u4ee5\u662f\u53ea\u8bfb\u3002<br>\u5982\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>adminAuth: {\n    type: \"credentials\",\n    users: &#91; <em>\/* list of users *\/<\/em> ],\n    default: {\n        permissions: \"read\"\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6765 * \u548c read \u4e24\u79cd\u5927\u6a21\u5757\u7684\u6743\u9650\u9650\u5b9a\u5916\uff0c\u7ba1\u7406\u5458\u8fd8\u53ef\u4ee5\u914d\u7f6e<a href=\"https:\/\/nodered.org\/docs\/api\/admin\/\" rel=\"noopener noreferrer nofollow\">Admin API<\/a><br>\u4f8b\u5982\u4e3a\u4e86\u83b7\u53d6\u5f53\u524d\u6d41\u7684\u4fe1\u606f\uff0c\u7528\u6237\u5c06\u4f1a\u8981\u6c42&nbsp;<code>flow.read<\/code>\u6743\u9650\u3002\u5982\u679c\u8981\u66f4\u65b0\u6d41\u4fe1\u606f\uff0c\u4ed6\u4eec\u9700\u8981\u4f7f\u7528<code>flows.write<\/code>\u6743\u9650\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u81ea\u5b9a\u4e49\u7528\u6237\u9274\u6743<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u76ee\u524d\u4ecb\u7ecd\u7684\u7528\u6237\u9274\u6743\u90fd\u662f\u786c\u7f16\u7801\u7684\u9274\u6743\uff0c\u5199\u6b7b\u7528\u6237\u6570\u636e\u53ca\u5bf9\u4e8e\u7684\u6743\u9650\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u65b9\u4fbf\u6269\u5c55\uff0c\u6240\u4ee5node-red\u53c8\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u65b9\u5f0f\uff0c\u81ea\u5b9a\u4e49\u7528\u6237\u9274\u6743\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u6b65\u9aa4<br>\u521b\u5efa&nbsp;<code>&lt;node-red&gt;\/user-authentication.js<\/code>&nbsp;\u6309\u7167\u4ee5\u4e0b\u6a21\u7248\u7f16\u5199\u6388\u6743\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {\n   type: \"credentials\",\n   users: function(username) {\n       return new Promise(function(resolve) {\n           <em>\/\/ Do whatever work is needed to check username is a valid<\/em>\n           <em>\/\/ user.<\/em>\n           if (valid) {\n               <em>\/\/ Resolve with the user object. It must contain<\/em>\n               <em>\/\/ properties 'username' and 'permissions'<\/em>\n               var user = { username: \"admin\", permissions: \"*\" };\n               resolve(user);\n           } else {\n               <em>\/\/ Resolve with null to indicate this user does not exist<\/em>\n               resolve(null);\n           }\n       });\n   },\n   authenticate: function(username,password) {\n       return new Promise(function(resolve) {\n           <em>\/\/ Do whatever work is needed to validate the username\/password<\/em>\n           <em>\/\/ combination.<\/em>\n           if (valid) {\n               <em>\/\/ Resolve with the user object. Equivalent to having<\/em>\n               <em>\/\/ called users(username);<\/em>\n               var user = { username: \"admin\", permissions: \"*\" };\n               resolve(user);\n           } else {\n               <em>\/\/ Resolve with null to indicate the username\/password pair<\/em>\n               <em>\/\/ were not valid.<\/em>\n               resolve(null);\n           }\n       });\n   },\n   default: function() {\n       return new Promise(function(resolve) {\n           <em>\/\/ Resolve with the user object for the default user.<\/em>\n           <em>\/\/ If no default user exists, resolve with null.<\/em>\n           resolve({anonymous: true, permissions:\"read\"});\n       });\n   }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u5728setting\u6587\u4ef6\u7684adminAuth\u4e2d\u914d\u7f6e<br><code>adminAuth: require(\".\/user-authentication\")<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u7ed3\u8bed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u5c31\u662f\u672c\u7bc7\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4e8e\u60f3\u8981\u7cbe\u7ec6\u5316\u63a7\u5236node-red\u7684\u6743\u9650\u5c24\u4e3a\u91cd\u8981\uff0c\u7279\u522b\u662f\u8981\u5c06\u5176\u6539\u9020\u4e3a\u4e00\u4e2a\u591a\u79df\u6237\u7cfb\u7edf\u65f6\u3002 \u8c22\u8c22\u89c2\u770b\u3002<\/p>\n<span id=\"magicpostMarker\"><\/span>","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u9ed8\u8ba4\u5730\uff0cnode-red\u7f16\u8f91\u5668\u53ef\u4ee5\u88ab\u4efb\u4f55\u8bbf\u95ee\u7684\u7528\u6237\u64cd\u4f5c&#8230;<\/p>","protected":false},"author":106,"featured_media":27977,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[1381],"tags":[1403],"class_list":["post-11524","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-red","tag-node-red"],"_links":{"self":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/comments?post=11524"}],"version-history":[{"count":0,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/posts\/11524\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media\/27977"}],"wp:attachment":[{"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/media?parent=11524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/categories?post=11524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modbus.cn\/en\/wp-json\/wp\/v2\/tags?post=11524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}