ThinkPHP6ÖÐÔõÑùʹÓÃAuthÈÏ֤ȨÏÞ²Ù×÷£¿
Ëæ×Å»¥ÁªÍøÓ¦ÓõÄÉú³¤£¬È¨ÏÞÖÎÀíÒѾ³ÉΪÁËÓ¦Óÿª·¢Öв»¿É»òȱµÄÒ»²¿·Ö¡£ÔÚ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇÐèÒª¶Ô²î±ðµÄÓû§·ÖÅɲî±ðµÄȨÏÞ£¬ÒÔµÖ´ïÊý¾ÝÇå¾²ºÍ²Ù×÷ȨÏÞ¿ØÖƵÄÄ¿µÄ¡£¶øthinkphp6¿ò¼ÜÖеÄauthÈÏ֤ȨÏÞ²Ù×÷£¬ÎªÎÒÃÇÌṩÁËÒ»ÖÖ¼òÆÓÒ×ÓõĽâ¾ö¼Æ»®¡£
ʲôÊÇAuthÈÏ֤ȨÏÞ²Ù×÷£¿
AuthÊÇThinkPHP6¿ò¼ÜÖеÄÒ»¿îȨÏÞÖÎÀí²å¼þ£¬Ëüͨ¹ý¶Ô½ÇÉ«¡¢Óû§¡¢È¨ÏÞ¡¢¹æÔòµÈ¾ÙÐнç˵ÓëÖÎÀí£¬ÊµÏÖÁËÒ»Ì׸ßЧ¿É¿ØµÄȨÏÞÖÎÀí»úÖÆ¡£ËüµÄÓŵãÔÚÓÚ²Ù×÷¼òÆÓ¡¢Ë³Ó¦ÐÔÇ¿¡¢¿É¿ØÐԸߡ¢¿ÉÀ©Õ¹ÐԺõÈÌص㣬ÔÚÆóÒµ¼¶Ó¦ÓÃÖÐÓ¦ÓÃÆձ顣
ÔÚÕâÀïÎÒÃǽâ˵һÏÂThinkPHP6ÖÐÔõÑùʹÓÃAuthʵÏÖȨÏÞÖÎÀí¡£
ÈÏ֤ȨÏÞ»úÖÆ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚʹÓÃAuthÇ°£¬ÎÒÃÇÐèÒªÏàʶһÏÂËüµÄÈÏ֤ȨÏÞ»úÖÆ¡£ËüµÄ½ÇÉ«¡¢Óû§¡¢È¨ÏÞ¡¢¹æÔòµÈ¿´·¨µÄ½ç˵ÈçÏ£º
½ÇÉ«£¨Role£©£ºÊÇָΪһ¸ö»ò¶à¸öÓû§·ÖÅÉȨÏÞµÄÒ»ÖÖÔÊÐíµ¥Î»¡£
Óû§£¨User£©£ºÏµÍ³ÖеÄÒ»¸ö¸öÌ壬¿ÉÒÔÊÇÒ»¸öÕæÊÊÓû§£¬Ò²¿ÉÒÔÊÇÒ»¸öϵͳÕË»§¡£
ȨÏÞ£¨Permission£©£ºÖ¸ÎªÓû§£¨»ò½ÇÉ«£©ÊÚȨµÄ²Ù×÷ÐÐΪ£¬ÀàËÆÓÚÊý¾Ý¿âÖеÄȨÏÞÉèÖá£
¹æÔò£¨Rule£©£ºÖ¸ÎªÈ¨ÏÞÉèÖÃһЩÏÞÖÆÌõ¼þ£¬ÀýÈ磺ʱ¼ä¶ÎÏÞÖÆ¡¢ÏÞÖÆIPµÈ¡£
ÏêϸÀ´Ëµ£¬ÎÒÃÇ¿ÉÒÔ½«Ò»Ð©³£¼ûµÄȨÏÞ½ç˵ÈçÏ£º
²Ëµ¥È¨ÏÞ£ºÖ¸¶ÔÓ¦ÓõÄij¸ö²Ëµ¥¾ÙÐлá¼û¿ØÖÆ£¬¿ÉÒÔÈòî±ðµÄÓû§¿´µ½²î±ðµÄ²Ëµ¥Ïî¡£
²Ù×÷ȨÏÞ£ºÖ¸¶ÔÓ¦ÓõÄij¸ö²Ù×÷ÐÐΪ¾ÙÐлá¼û¿ØÖÆ£¬¿ÉÒÔÈòî±ðµÄÓû§ÓµÓвî±ðµÄ²Ù×÷ȨÏÞ¡£
Êý¾ÝȨÏÞ£ºÖ¸¶ÔÓ¦ÓÃÖÐij¸öÊý¾Ý»á¼û¾ÙÐпØÖÆ£¬¿ÉÒÔÏÞÖÆÓû§Ö»ÄÜ¿´µ½»òÐÞ¸Ä×Ô¼ºËù¹ØÁªµÄÊý¾Ý¡£
×Ö¶ÎȨÏÞ£ºÖ¸¶ÔÓ¦ÓÃÖÐij¸ö×ֶξÙÐлá¼û¿ØÖÆ£¬¿ÉÒÔÏÞÖÆÓû§Ö»ÄÜ¿´µ½»òÐÞ¸Ä×Ô¼ºËù¹ØÁªµÄ×ֶΡ£
ÈÏ֤ȨÏÞ²Ù×÷ʵÏÖ
ÓÐÁËÈÏ֤ȨÏÞ»úÖƵĻù±¾¿´·¨£¬ÎÒÃǾͿÉÒÔ×îÏÈThinkPHP6ÖеÄAuthÈÏ֤ȨÏÞ²Ù×÷ÁË¡£
×°ÖÃAuth²å¼þ
Ê×ÏÈÎÒÃÇÐèҪȷ±£ÔÚÓ¦ÓÃÖÐÒѾװÖÃAuth²å¼þ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°Öãº
composer require topthink/think-auth
µÇ¼ºó¸´ÖÆ
ÒýÈëAuthÖÐÑë¼þ
ÔÚÓ¦ÓÃÖÐʹÓÃAuthÐèҪʹÓÃÖÐÑë¼þ£¬ÔÚconfig/middleware.phpÖоÙÐÐÉèÖãº
return [ //È«¾ÖÖÐÑë¼þÁбí 'global' => [ hinkmiddlewareSessionInit::class, hinkmiddlewareLoadLangPack::class, hinkmiddlewareCheckRequestCache::class, hinkmiddlewareSendFile::class, ], //ÖÐÑë¼þÓÖÃû 'alias' => [ 'auth' => hinkmiddlewareAuth::class, ], ];
µÇ¼ºó¸´ÖÆ
½ç˵ȨÏÞ¹æÔò
ÔÚÓ¦Óóõʼ»¯Ê±£¬ÎÒÃÇÐèÒª½ç˵һЩȨÏÞ¹æÔò£¬ÕâЩ¹æÔò½«±»ÓÃÓÚȨÏÞÈÏÖ¤£¬ÀýÈ磺
use thinkacadeAuth; //½ç˵¹æÔò Auth::rule('admin.user/index', 'checkAdmin'); Auth::rule('admin.user/add', 'checkAdmin'); Auth::rule('admin.user/edit', 'checkAdmin'); Auth::rule('admin.user/del', 'checkAdmin');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵ÁË»ù±¾µÄÓû§ÖÎÀíȨÏÞ¹æÔò£¬ÈôÊÇûÓÐÕâЩȨÏÞÔòÎÞ·¨»á¼û¶ÔÓ¦µÄ¿ØÖÆÆ÷²Ù×÷¡£
½ÇÉ«ºÍÓû§ÊÚȨ
ÎÒÃÇÐèÒªÔÚÓ¦ÓÃÖÐΪ½ÇÉ«ºÍÓû§¾ÙÐÐÊÚȨ£¬¿ÉÒÔÔÚÓ¦Óóõʼ»¯Ê±¾ÙÐÐÊÚȨ£º
//½ç˵½ÇÉ« Auth::group('admin', function () { //ÉèÖýÇɫȨÏÞ Auth::setRule([ 'admin.user/index', 'admin.user/add', 'admin.user/edit', 'admin.user/del', ]); }); //½ç˵Óû§²¢ÊÚȨ Auth::user('admin', function () { Auth::addToGroup('admin')//Ìí¼Ó½ÇÉ« ->addPermission(['admin.user/add'])//Ìí¼ÓȨÏÞ ->removePermission(['admin.user/del']);//ÒƳýȨÏÞ });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽç˵һ¸öÃûΪadminµÄ½ÇÉ«£¬²¢ÉèÖöÔÓ¦µÄȨÏÞ¹æÔò¡£È»ºóÎÒÃÇÓÖ½ç˵һ¸öÃûΪadminµÄÓû§£¬¸ÃÓû§ÓµÓÐÁËadmin½ÇÉ«£¬²¢¶Ôadmin.user/addȨÏÞ¾ÙÐÐÁËÊÚȨ£¬¶Ôadmin.user/delȨÏÞ¾ÙÐÐÁËÒƳý¡£
¾ÙÐÐÈÏÖ¤
ÔÚÎÒÃǾÙÐÐȨÏÞÈÏ֤ǰ£¬¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐÏȾÙÐнÇÉ«¡¢Óû§¡¢È¨Ï޵ȵÄÅжϣº
use thinkacadeAuth; class User extends Controller { //¾ÙÐÐÈÏÖ¤ public function index() { //ÑéÖ¤Óû§ÊÇ·ñµÇ¼£¬Ã»ÓеǼÔòÌøתµ½µÇ¼ҳÃæ if (!Auth::check()) { return redirect('admin/auth/login'); } //ÑéÖ¤ÊÇ·ñΪ³¬µÈÖÎÀíÔ±£¬ÊÇÔòÖ±½Ó·ÅÐÐ if (Auth::isSuperAdmin()) { return $this->view->assign('username', Auth::getUser()['username'])->fetch(); } //ÑéÖ¤ÊÇ·ñΪÖÎÀíÔ±½ÇÉ«£¬ÊÇÔòÑé֤ȨÏÞ£¬²»È»Ìøתµ½ÆäËûÒ³Ãæ if (Auth::group('admin')->check()) { if (Auth::check('admin.user/index')) { return $this->view->assign('username', Auth::getUser()['username'])->fetch(); } else { return redirect('admin/index/model_error'); } } else { return redirect('admin/index/role_error'); } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǾÙÐÐÁËÓû§µÇ¼ÑéÖ¤¡¢³¬µÈÖÎÀíÔ±ÑéÖ¤¡¢ÒÔ¼°½ÇÉ«ºÍȨÏÞÑéÖ¤µÈ²Ù×÷£¬×îÖÕ·µ»Ø¶ÔÓ¦µÄÒ³Ãæ»ò¾ÙÐÐÌøת¡£
×ܽá
ͨ¹ýÒÔÉϵIJÙ×÷£¬ÎÒÃÇ¿ÉÒÔʹÓÃThinkPHP6¿ò¼ÜÖеÄAuthÈÏ֤ȨÏÞ²Ù×÷ʵÏÖ¼òÆÓ¡¢¸ßЧ¡¢¿É¿ØµÄÓ¦ÓóÌÐòȨÏÞÖÎÀí£¬°ü¹ÜÊý¾ÝÇå¾²ºÍ²Ù×÷ȨÏÞ¿ØÖÆ¡£ÔÚʹÓÃÀú³ÌÖУ¬ÎªÁË°ü¹ÜÓ¦ÓóÌÐòµÄÇå¾²ÐÔ£¬ÎÒÃÇÐèÒª×ÐϸÉèÖÃÖÖÖÖȨÏÞ¹æÔò£¬ÏÞÖƶÔÓ¦µÄÓû§ºÍ½ÇɫȨÏÞ£¬Ìá¸ßÓ¦ÓóÌÐòµÄ¿ÉÀ©Õ¹ÐԺͿɿØÐÔ¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʹÓÃAuthÈÏ֤ȨÏÞ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡