{"id":151723,"date":"2020-08-27T05:09:50","date_gmt":"2020-08-26T21:09:50","guid":{"rendered":"http:\/\/4563.org\/?p=151723"},"modified":"2020-08-27T05:09:50","modified_gmt":"2020-08-26T21:09:50","slug":"rust%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6casbin-rust-%e4%b8%8b%e6%88%90%e7%86%9f%e5%a5%bd%e7%94%a8%e7%9a%84%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6%e5%ba%93","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=151723","title":{"rendered":"[Rust][\u6743\u9650\u63a7\u5236][Casbin] Rust \u4e0b\u6210\u719f\u597d\u7528\u7684\u6743\u9650\u63a7\u5236\u5e93"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [Rust][\u6743\u9650\u63a7\u5236][Casbin] Rust \u4e0b\u6210\u719f\u597d\u7528\u7684\u6743\u9650\u63a7\u5236\u5e93               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hackerchai <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u4ec0\u4e48\u662fCasbin-rs<\/h1>\n<p> <\/p>\n<p>Casbin\u662f\u7f57\u9633\u535a\u58eb\u4e3b\u5bfc\u5f00\u53d1\u7684\u57fa\u4e8e Go \u8bed\u8a00\u7684\u6743\u9650\u63a7\u5236\u5e93\u3002\u5b83\u652f\u6301 ACL, RBAC, ABAC \u7b49\u5e38\u7528\u7684\u8bbf\u95ee\u63a7\u5236\u6a21\u578b\u3002<\/p>\n<p>Casbin-rs\u5219\u662f Rust \u8bed\u8a00\u4e0b\u7684\u79fb\u690d\uff0c \u76f8\u6bd4 Go \u8bed\u8a00\u7248\u672c\u6709\u66f4\u9ad8\u7684\u901f\u5ea6\u548c\u5185\u5b58\u5b89\u5168\u4fdd\u969c\u3002<\/p>\n<h1>Casbin \u505a\u4e86\u4ec0\u4e48<\/h1>\n<p> <\/p>\n<ol>\n<li>Casbin \u7684\u914d\u7f6e\u6587\u4ef6\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff0c \u4e00\u4e2a\u662f Configuration \u6587\u4ef6\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u6a21\u578b\u914d\u7f6e\u6587\u4ef6\uff09\uff0c \u914d\u7f6e\u4e86\u6a21\u578b\uff08 Model \uff09\u9009\u7528\uff0c\u5206\u7ec4\uff08 Group \uff09\u914d\u7f6e\uff0c\u5b9a\u4e49\u8bf7\u6c42\uff08 Request \uff09\u548c\u7b56\u7565\uff08 Policy \uff09\u7ed3\u6784\uff0c\u518d\u6709\u5c31\u662f\u5339\u914d\u5668\uff08 Matcher \uff09\u7684\u914d\u7f6e\uff0c\u8fd9\u4e9b\u5728\u540e\u6587\u7531\u53d9\u8ff0\u3002\u53e6\u5916\u4e00\u4e2a\u5c31\u662f\u7b56\u7565\uff08 Policy \uff09\u7684\u76db\u653e\u5bb9\u5668\uff0c \u8fd9\u4e2a\u53ef\u4ee5\u662f csv \u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u636e\u5e93\uff08 MySQL\/PostgreSQl \uff09\u3002\u5bb9\u5668\u4e2d\u7684 Policies \u90fd\u884d\u751f\u4e8e Model \u7684\u914d\u7f6e<\/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<\/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<\/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 \/book\/1 \u53ef\u4ee5\u6620\u5c04\u5230 \/book\/:id<\/li>\n<\/ol>\n<h1>Casbin \u4e0d\u505a\u4ec0\u4e48<\/h1>\n<p> <\/p>\n<ol>\n<li>\u8eab\u4efd\u8ba4\u8bc1 authentication(\u5373\u9a8c\u8bc1\u7528\u6237\u7684\u7528\u6237\u540d\u3001\u5bc6\u7801)\uff0ccasbin \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 casbin \u8fdb\u884c\u8bbf\u95ee\u63a7\u5236\uff0c\u4e8c\u8005\u662f\u76f8\u4e92\u914d\u5408\u7684\u5173\u7cfb\u3002<\/li>\n<li>\u7ba1\u7406\u7528\u6237\u5217\u8868\u6216\u89d2\u8272\u5217\u8868\u3002Casbin \u8ba4\u4e3a\u7531\u9879\u76ee\u81ea\u8eab\u6765\u7ba1\u7406\u7528\u6237\u3001\u89d2\u8272\u5217\u8868\u66f4\u4e3a\u5408\u9002\uff0c\u7528\u6237\u901a\u5e38\u6709\u4ed6\u4eec\u7684\u5bc6\u7801\uff0c\u4f46\u662f Casbin \u7684\u8bbe\u8ba1\u601d\u60f3\u5e76\u4e0d\u662f\u628a \u5b83\u4f5c\u4e3a\u4e00\u4e2a\u5b58\u50a8\u5bc6\u7801\u7684\u5bb9\u5668\u3002\u800c\u662f\u5b58\u50a8 RBAC \u65b9\u6848\u4e2d\u7528\u6237\u548c\u89d2\u8272\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/li>\n<\/ol>\n<h1>\u4e00\u4e2a\u4f8b\u5b50<\/h1>\n<p> <\/p>\n<h2>\u6a21\u578b\u914d\u7f6e<\/h2>\n<pre><code>\/\/ model.conf # Request definition [request_definition] r = sub, obj, act  # Policy definition [policy_definition] p = sub, obj, act  # Policy effect [policy_effect] e = some(where (p.eft == allow))  # Matchers [matchers] m = r.sub == p.sub &amp;&amp; r.obj == p.obj &amp;&amp; r.act == p.act <\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6a21\u578b\u5b9a\u4e49\u6587\u4ef6\uff0c \u5176\u4e2d<code>sub<\/code>\u4ee3\u8868\u8bbf\u95ee\u8d44\u6e90\u7684\u7528\u6237\uff0c <code>obj<\/code>\u8868\u793a\u8981\u8bbf\u95ee\u7684\u8d44\u6e90\uff0c <code>act<\/code>\u8868\u793a\u5bf9\u8d44\u6e90\u6267\u884c\u7684\u64cd\u4f5c\u3002\u5982\u679c\u5728 Web \u7684\u60c5\u5f62\u4e2d\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0csub \u5bf9\u5e94\u7528\u6237\u540d\uff0cobj \u5bf9\u5e94\u8bbf\u95ee\u7684 URL Path\uff0cact \u4ee3\u8868 HTTP \u52a8\u4f5c(GET\/POST\/PUT).<\/p>\n<p>\u5728\u8fd9\u91cc\uff0cRequest Definition \u544a\u8bc9\u6211\u4eec\u8bf7\u6c42\u662f\u4ec0\u4e48\u6784\u6210\uff0c\u4e00\u5171\u4e09\u4e2a\u3002Policy Defination \u6709\u4ec0\u4e48\u6784\u6210\uff0c\u548c\u524d\u9762\u7684\u540c\u7406\u3002Policy Effect \u544a\u8bc9\u6211\u4eec\u4ec0\u4e48\u65f6\u5019\u89c4\u5219\u662f\u6709\u6548\u7684\uff0c\u800c Matcher \u544a\u8bc9\u6211\u4eec\u5f53\u8bf7\u6c42\u548c\u7b56\u7565\u6ee1\u8db3\u4e00\u5b9a\u5173\u7cfb\u624d\u53ef\u4ee5\u8fd4\u56de\u771f\uff08\u5141\u8bb8\u64cd\u4f5c\uff09\u3002\u5982\u4e0a\uff0c\u610f\u601d\u5c31\u5f88\u660e\u767d\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u8981\u52a0\u5165\u4e00\u4e2a\u8d85\u7ea7\u7ba1\u7406\u5458\uff0c\u5b83\u53ef\u4ee5\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code>[matchers] m = r.sub == p.sub &amp;&amp; r.obj == p.obj &amp;&amp; r.act == p.act || r.sub == \"root\" <\/code><\/pre>\n<h2>\u7b56\u7565\u914d\u7f6e<\/h2>\n<pre><code>p, alice, data1, read p, bob, data2, write <\/code><\/pre>\n<p>\u4e0a\u8ff0\u7684\u914d\u5408\u4e0a\u6587\u7684\u6a21\u578b\u914d\u7f6e\u8868\u8fbe\u7684\u610f\u601d\u5c31\u662f\uff0calice \u53ef\u4ee5\u8bfb data1\uff0cbob \u53ef\u4ee5\u5199 data2<\/p>\n<h1>Casbin Rust \u751f\u6001<\/h1>\n<p> <\/p>\n<p>\u4e3b\u4ed3\u5e93:<\/p>\n<p>Casbin-RS: \u76ee\u524d\u652f\u6301\u6240\u6709 Casbin Go \u7248\u672c\u652f\u6301\u7684\u7279\u6027\uff0c\u6b63\u5728\u6d3b\u8dc3\u5f00\u53d1\u4e2d<\/p>\n<p>\u76ee\u524d Casbin Rust \u6b63\u7a33\u6b65\u53d1\u5c55\u4e2d\uff0c\u76ee\u524d\u652f\u6301\u7684\u7ec4\u4ef6\u6709\uff1a<\/p>\n<ul>\n<li>Casbin Diesel Adaper: \u4f7f\u7528 Rust \u76ee\u524d\u6700\u706b\u7684 ORM \u7c7b\u5e93\u5f00\u53d1\u7684\u9002\u914d\u5668\uff0c\u652f\u6301 MySQL\/PostgreSQL\/SQLite<\/li>\n<li>Casbin Actix-web Middleware: Rust \u6700\u7531\u540d\u6c14\u7684 Web \u6846\u67b6\u5f53\u5c5e Actix-web\uff0c\u6027\u80fd\u9738\u699c\u3002Casbin \u652f\u6301 Actix \u4e2d\u95f4\u4ef6,\u81ea\u52a8\u4e3a\u8bf7\u6c42\u8fdb\u884c\u6743\u9650\u7ba1\u7406<\/li>\n<li>Casbin Actix-web Actor: Actix \u6846\u67b6\u4e0b\u5bf9 Casbin \u8fdb\u884c\u4e8c\u6b21\u5c01\u88c5\uff0c\u65b9\u4fbf\u5728 Actix-web \u4e2d\u4f7f\u7528\uff0c\u5c01\u88c5\u4e86\u5e38\u7528\u51fd\u6570<\/li>\n<li>Casbin Sqlx Adapter: \u652f\u6301\u5b8c\u5168\u5f02\u6b65\u7684\u6570\u636e\u5e93\u4e2d\u95f4\u4ef6\uff0c\u6027\u80fd\u66f4\u597d\uff0c\u57fa\u4e8e Sqlx \u3002\u652f\u6301 MySQL\/PostgreSQL<\/li>\n<\/ul>\n<p>\u57fa\u4e8e Actix-web \u5f00\u53d1\uff0c\u4f7f\u7528 Casbin \u4e2d\u95f4\u4ef6\u9274\u6743\uff0c \u4f7f\u7528 JWT \u7528\u6237\u6388\u6743\u7684\u4f8b\u5b50\uff1a<\/p>\n<ul>\n<li>Casbin Actix-web Real World App<\/li>\n<\/ul>\n<p>\u6b64\u5916 Casbin \u62e5\u6709\u5f3a\u5927\u7684\u6587\u6863\u652f\u6301\u548c\u793e\u533a\u4f9d\u6258\uff1a<\/p>\n<ul>\n<li>Casbin \u5b98\u7f51 Casbin \u6587\u6863<\/li>\n<li>Casbin \u8bba\u575b<\/li>\n<li>Casbin \u540c\u65f6\u652f\u6301\u9664\u4e86 Go\uff0cRust \u4ee5\u5916\u516d\u79cd\u8bed\u8a00\uff1aMode.js, PHP, Python, C#(.NET), C++, Java<\/li>\n<\/ul>\n<p><strong>\u6700\u540e\u5e0c\u671b\u5404\u4f4d\u770b\u5b98\u8d70\u8fc7\u8def\u8fc7\uff0c\u522b\u5fd8\u4e86\u7ed9\u4e00\u4e2a Star \u652f\u6301\u4e00\u4e0b\u6211\u4eec\u7684\u5f00\u53d1<\/strong><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3158228\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Hackerchai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Casbin \u662f\u4eca\u5e74 Google Summer of Code \u5165\u9009\u7684\u5f00\u6e90\u793e\u533a\u4e2d\u552f\u4e00\u7684\u4e00\u4e2a\u7531\u56fd\u4eba\u53d1\u8d77\u7684\u5f00\u6e90\u793e\u533a\uff0c\u540c\u65f6\u4e5f\u662f\u5386\u53f2\u4e0a\u7684\u7b2c\u4e00\u4e2a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3158229\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : del1214 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u652f\u6301 rust \u9879\u76ee                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3158230\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Hackerchai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @del1214 \u8c22\u8c22\u8001\u54e5\u7684\u652f\u6301                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3158231\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zjupigeon <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Hackerchai \u540c\u662f\u6885\u897f\u8ff7\uff0c\u540c\u662f rust \u8ff7\uff0c\u9876                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3158232\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guotie <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Casbin \u662f\u4e2a\u597d\u4e1c\u897f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[Rust][\u6743\u9650\u63a7\u5236][Casb&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\/151723"}],"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=151723"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/151723\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151723"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}