{"id":23688,"date":"2024-11-21T09:06:40","date_gmt":"2024-11-21T01:06:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23688\/"},"modified":"2024-11-21T09:06:40","modified_gmt":"2024-11-21T01:06:40","slug":"%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8auth%e6%8e%88%e6%9d%83%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23688\/","title":{"rendered":"\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u6210\u4e3a\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u95ee\u9898\uff0c\u5982\u4f55\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u5df2\u7ecf\u6210\u4e3a\u6240\u6709\u5f00\u53d1\u8005\u9762\u4e34\u7684\u96be\u9898\u3002auth\u6388\u6743\u6280\u672f\u662f\u4e00\u79cd\u6d41\u884c\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u4e00\u4e0bAuth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u6838\u5fc3\u6982\u5ff5\u3002<\/p>\n<ol>\n<li>Auth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406<\/li>\n<\/ol>\n<p>Auth\u6388\u6743\u7684\u6838\u5fc3\u662f\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236\uff0c\u5b83\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u4e09\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<p>1.1 \u521b\u5efa\u89d2\u8272<\/p>\n<p>\u5728\u4f7f\u7528Auth\u6388\u6743\u524d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u89d2\u8272\u3002\u89d2\u8272\u662f\u4e00\u7ec4\u6743\u9650\u7684\u96c6\u5408\uff0c\u5b83\u5b9a\u4e49\u4e86\u7528\u6237\u62e5\u6709\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>1.2 \u5206\u914d\u6743\u9650\u7ed9\u89d2\u8272<\/p>\n<p>\u5728\u521b\u5efa\u89d2\u8272\u4e4b\u540e\uff0c\u9700\u8981\u5c06\u76f8\u5e94\u7684\u6743\u9650\u5206\u914d\u7ed9\u89d2\u8272\u3002\u6743\u9650\u662f\u6307\u53ef\u4ee5\u8bbf\u95ee\u54ea\u4e9b\u529f\u80fd\u6a21\u5757\u6216\u6570\u636e\u7684\u6388\u6743\u3002<\/p>\n<p>1.3 \u5c06\u89d2\u8272\u5206\u914d\u7ed9\u7528\u6237<\/p>\n<p>\u6700\u540e\uff0c\u9700\u8981\u5c06\u89d2\u8272\u5206\u914d\u7ed9\u7528\u6237\u3002\u4e00\u4e2a\u7528\u6237\u53ef\u4ee5\u88ab\u5206\u914d\u591a\u4e2a\u89d2\u8272\uff0c\u8fd9\u4e9b\u89d2\u8272\u5c31\u51b3\u5b9a\u4e86\u7528\u6237\u62e5\u6709\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<p>\u5728Auth\u6388\u6743\u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\uff0c\u4f7f\u7528Access\u63a7\u5236\u5668\u5b9e\u73b0\u8bbf\u95ee\u63a7\u5236\u3002Access\u63a7\u5236\u5668\u7528\u4e8e\u68c0\u67e5\u7528\u6237\u662f\u5426\u6709\u6743\u8bbf\u95ee\u5f53\u524d\u7684URL\uff0c\u5982\u679c\u7528\u6237\u6709\u8bbf\u95ee\u6743\u9650\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u8bbf\u95ee\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n<ol>\n<li>\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f<\/li>\n<\/ol>\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86Auth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u5177\u4f53\u8bf4\u660e\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u3002\u5047\u8bbe\u6211\u4eec\u7684\u540e\u53f0\u6709\u4e24\u79cd\u7528\u6237\uff1a\u7ba1\u7406\u5458\u548c\u666e\u901a\u7528\u6237\u3002\u7ba1\u7406\u5458\u53ef\u4ee5\u8bbf\u95ee\u6240\u6709\u7684\u5185\u5bb9\u6a21\u5757\uff0c\u800c\u666e\u901a\u7528\u6237\u53ea\u80fd\u8bbf\u95ee\u90e8\u5206\u5185\u5bb9\u3002<\/p>\n<p>2.1 \u5b89\u88c5\u5e76\u914d\u7f6eAuth\u63d2\u4ef6<\/p>\n<p>\u5728\u4f7f\u7528Auth\u6280\u672f\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5e76\u914d\u7f6e\u597dAuth\u63d2\u4ef6\u3002\u5728ThinkPHP6\u4e2d\uff0cAuth\u63d2\u4ef6\u5df2\u7ecf\u96c6\u6210\u5230\u4e86\u6846\u67b6\u4e2d\uff0c\u53ea\u9700\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728config\u76ee\u5f55\u4e0b\u521b\u5efaauth.php\u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6e\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre>return [\n    \/\/ \u7528\u6237\u8ba4\u8bc1\u7684\u7c7b\u540d\uff0c\u4e0d\u8bbe\u7f6e\u5219\u4f7f\u7528\u6838\u5fc3\u96c6\u6210\u8ba4\u8bc1\u65b9\u6cd5\n    'auth'     =&amp;gt; AppAuth::class,\n    \/\/ \u4e0d\u9700\u8981\u8ba4\u8bc1\u7684\u8def\u7531\uff0c\u53ef\u5141\u8bb8\u6240\u6709\u7528\u6237\u8bbf\u95ee\u7684\u8def\u7531\n    'no_auth'  =&amp;gt; ['index\/index'],\n    \/\/ \u9700\u8981\u8ba4\u8bc1\u4e14\u9a8c\u8bc1\u5931\u8d25\u65f6\u8df3\u8f6c\u7684\u5730\u5740\n    'fail_url' =&amp;gt; 'index\/login',\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.2 \u521b\u5efaUser\u6a21\u578b<\/p>\n<p>\u521b\u5efaUser\u6a21\u578b\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appmodel;\n\nuse thinkModel;\n\nclass User extends Model {\n\n    \/\/ \u5b9a\u4e49\u89d2\u8272\u5173\u8054\n    public function roles() {\n        return $this-&gt;belongsToMany(Role::class, 'user_role');\n    }\n\n    \/\/ \u5224\u65ad\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u5f53\u524d\u64cd\u4f5c\n    public function hasPermission($permission) {\n        foreach ($this-&amp;gt;roles as $role) {\n            if ($role-&amp;gt;checkPermission($permission)) {\n                return true;\n            }\n        }\n\n        return false;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.3 \u521b\u5efaRole\u6a21\u578b<\/p>\n<p>\u521b\u5efaRole\u6a21\u578b\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appmodel;\n\nuse thinkModel;\n\nclass Role extends Model {\n\n    \/\/ \u5b9a\u4e49\u6743\u9650\u5173\u8054\n    public function permissions() {\n        return $this-&gt;belongsToMany(Permission::class, 'role_permission');\n    }\n\n    \/\/ \u68c0\u67e5\u89d2\u8272\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u5f53\u524d\u64cd\u4f5c\n    public function checkPermission($permission) {\n        foreach ($this-&amp;gt;permissions as $item) {\n            if ($item-&amp;gt;name == $permission) {\n                return true;\n            }\n        }\n\n        return false;\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.4 \u521b\u5efaPermission\u6a21\u578b<\/p>\n<p>\u521b\u5efaPermission\u6a21\u578b\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appmodel;\n\nuse thinkModel;\n\nclass Permission extends Model {\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.5 \u521b\u5efa\u6570\u636e\u5e93\u8868<\/p>\n<p>\u521b\u5efa\u6570\u636e\u5e93\u8868\uff0c\u5305\u62ecuser\u8868\u3001role\u8868\u3001permission\u8868\u53ca\u4e24\u4e2a\u5173\u7cfb\u8868user_role\u3001role_permission\u3002<\/p>\n<p>user\u8868\u76f8\u5173\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Comment<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>username<\/td>\n<td>varchar(20)<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>password<\/td>\n<td>varchar(255)<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>created_at<\/td>\n<td>datetime<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>updated_at<\/td>\n<td>datetime<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>role\u8868\u76f8\u5173\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Comment<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>varchar(20)<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>created_at<\/td>\n<td>datetime<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>updated_at<\/td>\n<td>datetime<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>permission\u8868\u76f8\u5173\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Comment<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>name<\/td>\n<td>varchar(20)<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>user_role\u8868\u76f8\u5173\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Comment<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>user_id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>role_id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>role_permission\u8868\u76f8\u5173\u7ed3\u6784\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>Field<\/th>\n<th>Type<\/th>\n<th>Comment<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>role_id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>permission_id<\/td>\n<td>int<\/td>\n<td> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>2.6 \u63a7\u5236\u5668\u4ee3\u7801\u5b9e\u73b0<\/p>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u793a\u4f8b\u63a7\u5236\u5668\u6765\u8bf4\u660e\u5982\u4f55\u5b9e\u73b0Auth\u6388\u6743\u3002\u793a\u4f8b\u63a7\u5236\u5668\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>dmincontroller;\n\nuse appmodelUser;\nuse thinkController;\n\nclass Index extends Controller {\n\n    \/\/ \u540e\u53f0\u9996\u9875\n    public function index() {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\n        $user_id = session('user_id');\n        $user = User::find($user_id);\n\n        \/\/ \u5224\u65ad\u7528\u6237\u662f\u5426\u6709\u6743\u9650\u8bbf\u95ee\u5f53\u524d\u64cd\u4f5c\n        if (!$user-&amp;gt;hasPermission($this-&amp;gt;request-&amp;gt;path())) {\n            $this-&amp;gt;error('\u65e0\u6743\u8bbf\u95ee');\n        }\n\n        return view();\n    }\n\n    \/\/ \u767b\u5f55\u9875\u9762\n    public function login() {\n        return view();\n    }\n\n    \/\/ \u5904\u7406\u767b\u5f55\u8bf7\u6c42\n    public function do_login() {\n        $username = $this-&amp;gt;request-&amp;gt;param('username');\n        $password = $this-&amp;gt;request-&amp;gt;param('password');\n\n        \/\/ \u6839\u636e\u7528\u6237\u540d\u67e5\u8be2\u7528\u6237\n        $user = User::where('username', $username)-&amp;gt;find();\n\n        \/\/ \u9a8c\u8bc1\u7528\u6237\u5bc6\u7801\n        if ($user &amp;amp;&amp;amp; password_verify($password, $user-&amp;gt;password)) {\n            \/\/ \u8bb0\u5f55\u767b\u5f55\u72b6\u6001\n            session('user_id', $user-&amp;gt;id);\n\n            \/\/ \u8df3\u8f6c\u5230\u540e\u53f0\u9996\u9875\n            $this-&amp;gt;redirect('index\/index');\n        } else {\n            $this-&amp;gt;error('\u767b\u5f55\u5931\u8d25');\n        }\n    }\n\n    \/\/ \u9000\u51fa\u767b\u5f55\n    public function logout() {\n        session('user_id', null);\n        $this-&amp;gt;redirect('index\/login');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86Auth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406\u3001\u6838\u5fc3\u6982\u5ff5\u53ca\u5728ThinkPHP6\u4e2d\u7684\u5e94\u7528\u5b9e\u73b0\u3002\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u4e3a\u7528\u6237\u63d0\u4f9b\u66f4\u52a0\u5b89\u5168\u53ef\u9760\u7684\u670d\u52a1\u3002\u5728\u540e\u7eed\u7684Web\u5e94\u7528\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e5f\u5e94\u5f53\u6ce8\u91cd\u5b89\u5168\u6027\u7684\u4fdd\u969c\uff0c\u5c3d\u53ef\u80fd\u5730\u5229\u7528\u597d\u73b0\u6709\u7684\u5b89\u5168\u6280\u672f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u4e0d\u65ad\u53d1\u5c55\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u6210\u4e3a\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u95ee\u9898\uff0c\u5982\u4f55\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u5df2\u7ecf\u6210\u4e3a\u6240\u6709\u5f00\u53d1\u8005\u9762\u4e34\u7684\u96be\u9898\u3002auth\u6388\u6743\u6280\u672f\u662f\u4e00\u79cd\u6d41\u884c\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u63d0\u4f9b\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u4e00\u4e0bAuth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u6838\u5fc3\u6982\u5ff5\u3002 Auth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406 Auth\u6388\u6743\u7684\u6838\u5fc3\u662f\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236\uff0c\u5b83\u4e3b\u8981\u5206\u4e3a\u4ee5\u4e0b\u4e09\u4e2a\u6b65\u9aa4\uff1a 1.1 \u521b\u5efa\u89d2\u8272 \u5728\u4f7f\u7528Auth\u6388\u6743\u524d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u89d2\u8272\u3002\u89d2\u8272\u662f\u4e00\u7ec4\u6743\u9650\u7684\u96c6\u5408\uff0c\u5b83\u5b9a\u4e49\u4e86\u7528\u6237\u62e5\u6709\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1.2 \u5206\u914d\u6743\u9650\u7ed9\u89d2\u8272 \u5728\u521b\u5efa\u89d2\u8272\u4e4b\u540e\uff0c\u9700\u8981\u5c06\u76f8\u5e94\u7684\u6743\u9650\u5206\u914d\u7ed9\u89d2\u8272\u3002\u6743\u9650\u662f\u6307\u53ef\u4ee5\u8bbf\u95ee\u54ea\u4e9b\u529f\u80fd\u6a21\u5757\u6216\u6570\u636e\u7684\u6388\u6743\u3002 1.3 \u5c06\u89d2\u8272\u5206\u914d\u7ed9\u7528\u6237 \u6700\u540e\uff0c\u9700\u8981\u5c06\u89d2\u8272\u5206\u914d\u7ed9\u7528\u6237\u3002\u4e00\u4e2a\u7528\u6237\u53ef\u4ee5\u88ab\u5206\u914d\u591a\u4e2a\u89d2\u8272\uff0c\u8fd9\u4e9b\u89d2\u8272\u5c31\u51b3\u5b9a\u4e86\u7528\u6237\u62e5\u6709\u7684\u8bbf\u95ee\u6743\u9650\u3002 \u5728Auth\u6388\u6743\u7684\u5de5\u4f5c\u6d41\u7a0b\u4e2d\uff0c\u4f7f\u7528Access\u63a7\u5236\u5668\u5b9e\u73b0\u8bbf\u95ee\u63a7\u5236\u3002Access\u63a7\u5236\u5668\u7528\u4e8e\u68c0\u67e5\u7528\u6237\u662f\u5426\u6709\u6743\u8bbf\u95ee\u5f53\u524d\u7684URL\uff0c\u5982\u679c\u7528\u6237\u6709\u8bbf\u95ee\u6743\u9650\uff0c\u5219\u53ef\u4ee5\u7ee7\u7eed\u8bbf\u95ee\u76f8\u5173\u5185\u5bb9\u3002 \u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f \u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86Auth\u6388\u6743\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u5177\u4f53\u8bf4\u660e\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528Auth\u6388\u6743\u6280\u672f\u3002\u5047\u8bbe\u6211\u4eec\u7684\u540e\u53f0\u6709\u4e24\u79cd\u7528\u6237\uff1a\u7ba1\u7406\u5458\u548c\u666e\u901a\u7528\u6237\u3002\u7ba1\u7406\u5458\u53ef\u4ee5\u8bbf\u95ee\u6240\u6709\u7684\u5185\u5bb9\u6a21\u5757\uff0c\u800c\u666e\u901a\u7528\u6237\u53ea\u80fd\u8bbf\u95ee\u90e8\u5206\u5185\u5bb9\u3002 2.1 \u5b89\u88c5\u5e76\u914d\u7f6eAuth\u63d2\u4ef6 \u5728\u4f7f\u7528Auth\u6280\u672f\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5\u5e76\u914d\u7f6e\u597dAuth\u63d2\u4ef6\u3002\u5728ThinkPHP6\u4e2d\uff0cAuth\u63d2\u4ef6\u5df2\u7ecf\u96c6\u6210\u5230\u4e86\u6846\u67b6\u4e2d\uff0c\u53ea\u9700\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\u5373\u53ef\u4f7f\u7528\u3002 \u9996\u5148\uff0c\u5728config\u76ee\u5f55\u4e0b\u521b\u5efaauth.php\u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6e\u4fe1\u606f\u5982\u4e0b\uff1a return [ \/\/ \u7528\u6237\u8ba4\u8bc1\u7684\u7c7b\u540d\uff0c\u4e0d\u8bbe\u7f6e\u5219\u4f7f\u7528\u6838\u5fc3\u96c6\u6210\u8ba4\u8bc1\u65b9\u6cd5 &#8216;auth&#8217; =&amp;gt; AppAuth::class, \/\/ \u4e0d\u9700\u8981\u8ba4\u8bc1\u7684\u8def\u7531\uff0c\u53ef\u5141\u8bb8\u6240\u6709\u7528\u6237\u8bbf\u95ee\u7684\u8def\u7531 &#8216;no_auth&#8217; =&amp;gt; [&#8216;index\/index&#8217;], \/\/ \u9700\u8981\u8ba4\u8bc1\u4e14\u9a8c\u8bc1\u5931\u8d25\u65f6\u8df3\u8f6c\u7684\u5730\u5740 &#8216;fail_url&#8217; =&amp;gt; &#8216;index\/login&#8217;, ]; \u767b\u5f55\u540e\u590d\u5236 2.2 \u521b\u5efaUser\u6a21\u578b \u521b\u5efaUser\u6a21\u578b\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace appmodel; use thinkModel; class User extends Model { \/\/ \u5b9a\u4e49\u89d2\u8272\u5173\u8054 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23688","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23688"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23688\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}