{"id":173987,"date":"2020-10-26T05:12:19","date_gmt":"2020-10-25T21:12:19","guid":{"rendered":"http:\/\/4563.org\/?p=173987"},"modified":"2020-10-26T05:12:19","modified_gmt":"2020-10-25T21:12:19","slug":"casbin%ef%bc%9a%e6%94%af%e6%8c%81-acl%e3%80%81rbac%e3%80%81abac-%e5%a4%9a%e7%a7%8d%e6%a8%a1%e5%9e%8b%e7%9a%84-node-js-%e6%9d%83%e9%99%90%e7%ae%a1%e7%90%86%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=173987","title":{"rendered":"Casbin\uff1a\u652f\u6301 ACL\u3001RBAC\u3001ABAC \u591a\u79cd\u6a21\u578b\u7684 Node.js \u6743\u9650\u7ba1\u7406\u6846\u67b6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Casbin\uff1a\u652f\u6301 ACL\u3001RBAC\u3001ABAC \u591a\u79cd\u6a21\u578b\u7684 Node.js \u6743\u9650\u7ba1\u7406\u6846\u67b6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hsluoyz <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/10\/20201027_5f97d5be3d5d4.png\" alt=\"Casbin\uff1a\u652f\u6301 ACL\u3001RBAC\u3001ABAC \u591a\u79cd\u6a21\u578b\u7684 Node.js \u6743\u9650\u7ba1\u7406\u6846\u67b6\" \/> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/10\/20201027_5f97d5c2ccde5.png\" alt=\"Casbin\uff1a\u652f\u6301 ACL\u3001RBAC\u3001ABAC \u591a\u79cd\u6a21\u578b\u7684 Node.js \u6743\u9650\u7ba1\u7406\u6846\u67b6\" \/><\/p>\n<p>Node-Casbin \u662f\u4e00\u4e2a\u7528 Node.js \u8bed\u8a00\u6253\u9020\u7684\u8f7b\u91cf\u7ea7\u5f00\u6e90\u8bbf\u95ee\u63a7\u5236\u6846\u67b6\uff08 https:\/\/github.com\/casbin\/node-casbin )\uff0c\u76ee\u524d\u5728 GitHub \u5f00\u6e90\u3002Node-Casbin \u91c7\u7528\u4e86\u5143\u6a21\u578b\u7684\u8bbe\u8ba1\u601d\u60f3\uff0c\u652f\u6301\u591a\u79cd\u7ecf\u5178\u7684\u8bbf\u95ee\u63a7\u5236\u65b9\u6848\uff0c\u5982\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236 RBAC \u3001\u57fa\u4e8e\u5c5e\u6027\u7684\u8bbf\u95ee\u63a7\u5236 ABAC \u7b49\u3002<\/p>\n<h2>Node-Casbin \u7684\u4e3b\u8981\u7279\u6027\u5305\u62ec\uff1a<\/h2>\n<ol>\n<li>\u652f\u6301\u81ea\u5b9a\u4e49\u8bf7\u6c42\u7684\u683c\u5f0f\uff0c\u9ed8\u8ba4\u7684\u8bf7\u6c42\u683c\u5f0f\u4e3a{subject, object, action}\uff1b<\/li>\n<li>\u5177\u6709\u8bbf\u95ee\u63a7\u5236\u6a21\u578b model \u548c\u7b56\u7565 policy \u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1b<\/li>\n<li>\u652f\u6301 RBAC \u4e2d\u7684\u591a\u5c42\u89d2\u8272\u7ee7\u627f\uff0c\u4e0d\u6b62\u4e3b\u4f53\u53ef\u4ee5\u6709\u89d2\u8272\uff0c\u8d44\u6e90\u4e5f\u53ef\u4ee5\u5177\u6709\u89d2\u8272\uff1b<\/li>\n<li>\u652f\u6301\u8d85\u7ea7\u7528\u6237\uff0c\u5982 root \u6216 Administrator\uff0c\u8d85\u7ea7\u7528\u6237\u53ef\u4ee5\u4e0d\u53d7\u6388\u6743\u7b56\u7565\u7684\u7ea6\u675f\u8bbf\u95ee\u4efb\u610f\u8d44\u6e90\uff1b<\/li>\n<li>\u652f\u6301\u591a\u79cd\u5185\u7f6e\u7684\u64cd\u4f5c\u7b26\uff0c\u5982 keyMatch\uff0c\u65b9\u4fbf\u5bf9\u8def\u5f84\u5f0f\u7684\u8d44\u6e90\u8fdb\u884c\u7ba1\u7406\uff0c\u5982 \/foo\/bar \u53ef\u4ee5\u6620\u5c04\u5230 \/foo*\uff1b<\/li>\n<\/ol>\n<h2>Node-Casbin \u4e0d\u505a\u7684\u4e8b\u60c5\uff1a<\/h2>\n<ol>\n<li>\u8eab\u4efd\u8ba4\u8bc1 authentication \uff08\u5373\u9a8c\u8bc1\u7528\u6237\u7684\u7528\u6237\u540d\u3001\u5bc6\u7801\uff09\uff0cNode-Casbin \u53ea\u8d1f\u8d23\u8bbf\u95ee\u63a7\u5236\u3002\u5e94\u8be5\u6709\u5176\u4ed6\u4e13\u95e8\u7684\u7ec4\u4ef6\u8d1f\u8d23\u8eab\u4efd\u8ba4\u8bc1\uff0c\u7136\u540e\u7531 Node-Casbin \u8fdb\u884c\u8bbf\u95ee\u63a7\u5236\uff0c\u4e8c\u8005\u662f\u76f8\u4e92\u914d\u5408\u7684\u5173\u7cfb\uff1b<\/li>\n<li>\u7ba1\u7406\u7528\u6237\u5217\u8868\u6216\u89d2\u8272\u5217\u8868\u3002Node-Casbin \u8ba4\u4e3a\u7531\u9879\u76ee\u81ea\u8eab\u6765\u7ba1\u7406\u7528\u6237\u3001\u89d2\u8272\u5217\u8868\u66f4\u4e3a\u5408\u9002\uff0cNode-Casbin \u5047\u8bbe\u6240\u6709\u7b56\u7565\u548c\u8bf7\u6c42\u4e2d\u51fa\u73b0\u7684\u7528\u6237\u3001\u89d2\u8272\u3001\u8d44\u6e90\u90fd\u662f\u5408\u6cd5\u6709\u6548\u7684\u3002<\/li>\n<\/ol>\n<h2>\u5b89\u88c5<\/h2>\n<pre><code><span>npm<\/span> install casbin <\/code><\/pre>\n<h2>HelloWorld \u4f8b\u5b50<\/h2>\n<p>\u521d\u59cb\u5316\u4e00\u4e2a enforcer\uff0c\u4f20\u5165\u4e24\u4e2a\u53c2\u6570\uff1a\u6a21\u578b\u6587\u4ef6\u8def\u5f84\u548c\u7b56\u7565\u6587\u4ef6\u8def\u5f84\uff1b<\/p>\n<pre><code><span>const<\/span> enforcer = <span>new<\/span> Enforcer(<span>'path\/to\/model.conf'<\/span>, <span>'path\/to\/policy.csv'<\/span>); <\/code><\/pre>\n<p>\u5728\u4f60\u7684\u4ee3\u7801\u9700\u8981\u8fdb\u884c\u8bbf\u95ee\u63a7\u5236\u7684\u4f4d\u7f6e\uff0c\u52a0\u5165\u5982\u4e0b\u94a9\u5b50\uff1b<\/p>\n<pre><code><span>const<\/span> sub = <span>'alice'<\/span>; <span>\/\/ the user that wants to access a resource.<\/span> <span>const<\/span> obj = <span>'data1'<\/span>; <span>\/\/ the resource that is going to be accessed.<\/span> <span>const<\/span> act = <span>'read'<\/span>; <span>\/\/ the operation that the user performs on the resource.<\/span> <\/code><p><code><span>if<\/span> (enforcer.enforce(sub, obj, act) == <span>true<\/span>) { <span>\/\/ permit alice to read data1<\/span> } <span>else<\/span> { <span>\/\/ deny the request, show an error<\/span> } <\/code><\/p><\/pre>\n<\/p>\n<p>\u91c7\u7528\u7ba1\u7406 API \u8fdb\u884c\u6743\u9650\u7684\u7ba1\u7406\uff0c\u5982\u83b7\u53d6\u4e00\u4e2a\u7528\u6237\u6240\u6709\u7684\u89d2\u8272\uff1b<\/p>\n<pre><code><span>const<\/span> roles = enforcer.getRoles(<span>'alice'<\/span>); <\/code><\/pre>\n<p>\u8bf7\u53c2\u8003test\u76ee\u5f55\u83b7\u5f97\u66f4\u591a\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h2>\u793e\u533a\u8fdb\u5c55<\/h2>\n<p>Node-Casbin \u76ee\u524d\u6b63\u5728\u79ef\u6781\u5411\u793e\u533a\u8fdb\u884c\u63a8\u9001\uff0c\u76ee\u524d\u901a\u8fc7\u63d2\u4ef6\u7684\u65b9\u5f0f\u5df2\u7ecf\u652f\u6301\u4e0e Koa2 \u7b49 Web \u6846\u67b6\u8fdb\u884c\u96c6\u6210\uff0c\u5c06\u6765\u4f1a\u63a8\u5e7f\u5230\u66f4\u591a Web \u6846\u67b6\u4ee5\u53ca\u793e\u533a\u3002Casbin \u5df2\u7ecf\u6709 Golang \u7248\u672c\u3001Java \u7248\u672c\u548c PHP \u7248\u672c\u3002\u6709\u8de8\u8bed\u8a00\u9700\u6c42\u7684\u5f00\u53d1\u8005\u53ef\u4ee5\u53ea\u7528 Casbin \u8fd9\u4e00\u5957\u6846\u67b6\u5c31\u5b9e\u73b0\u591a\u4e2a\u4e0d\u540c\u8bed\u8a00\u7684\u9879\u76ee\u7684\u6743\u9650\u7ba1\u7406\u4efb\u52a1\u3002<\/p>\n<ul>\n<li>Casbin (Go)\uff1a https:\/\/github.com\/casbin\/casbin<\/li>\n<li>jCasbin (Java)\uff1a https:\/\/github.com\/casbin\/jcasbin<\/li>\n<li>PHP-Casbin (PHP)\uff1a https:\/\/github.com\/sstutz\/php-casbin<\/li>\n<li>Node-Casbin (Node.js): https:\/\/github.com\/casbin\/node-casbin<\/li>\n<\/ul>\n<h2>\u534f\u8bae<\/h2>\n<p>Node-Casbin \u91c7\u7528 Apache 2.0 \u5f00\u6e90\u534f\u8bae\u53d1\u5e03\u3002<\/p>\n<h2>\u8054\u7cfb\u4f5c\u8005<\/h2>\n<p>\u6709\u95ee\u9898\u8bf7\u63d0\u4ea4 Issues: https:\/\/github.com\/casbin\/node-casbin\/issues<\/p>\n<p>\u6216\u8005\u52a0\u5165 QQ \u7fa4\uff1a546057381\uff08 Casbin \u8bbf\u95ee\u63a7\u5236\u8ba8\u8bba\u7fa4\uff09<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3890245\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : YingJie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             npcap \u4f5c\u8005\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3890246\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hsluoyz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @YingJie npcap \u5f88\u706b\u4e48\uff1f\u4e3a\u5565\u90fd\u8bf4\u8fd9\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Casbin\uff1a\u652f\u6301 ACL\u3001RBA&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/173987"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=173987"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/173987\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173987"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}