laravelÖÐÑë¼þµÄʵÏÖÀú³Ì
Ëæ×Å»¥ÁªÍøºÍwebÓ¦ÓõÄÒ»Ö±Éú³¤£¬web¿ª·¢¿ò¼ÜÒ²Ò»Ö±Ó¿ÏÖ£¬ÆäÖУ¬laravel¿ò¼Ü»ñµÃÁËÆÕ±éµÄÈϿɺÍʹÓá£laravel¿ò¼ÜΪ¿ª·¢ÕßÌṩÁËÖÐÑë¼þÕâÒ»Ö÷ÒªµÄ¹¦Ð§£¬ÔÚÓ¦ÓÃÖÐͨ¹ýÖÐÑë¼þ¶ÔÇëÇó¾ÙÐйýÂ˺ʹ¦Àí£¬ÓÐÓÃÌá¸ßÁË¿ª·¢Ð§ÂʺÍÇå¾²ÐÔ¡£±¾ÎĽ«ÏêϸÏÈÈÝlaravelÖÐÑë¼þµÄʵÏÖÀú³Ì£¬ÒÔ×ÊÖú¿ª·¢Õ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃÕâÒ»¹¦Ð§¡£
Ò»¡¢Ê²Ã´ÊÇÖÐÑë¼þ
ÔÚLaravel¿ò¼ÜÖУ¬ÖÐÑë¼þ£¨Middleware£©Ö¸µÄÊǶԽøÈëÓ¦ÓõÄHTTPÇëÇó¾ÙÐйýÂ˺ʹ¦ÀíµÄÒ»ÖÖ»úÖÆ£¬ÀàËÆÓÚ´úÂëÖг£¼ûµÄ×èµ²Æ÷ºÍ¹ýÂËÆ÷µÄ¿´·¨¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬ÖÐÑë¼þ»á×öһЩͨÓõĴ¦ÀíÊÂÇ飬ºÃ±È£º
ÈÏÕæ¶ÔÇëÇó¾ÙÐÐÉí·ÝÑéÖ¤£»
¼ì²é²¢ÏÞÖÆÇëÇóµÄ»á¼ûƵÂÊ£»
ά»¤»á»°×´Ì¬£»
ƾ֤ÇëÇóÖеÄHeaderÐÅÏ¢£¬ÉèÖöÔÓ¦µÄÏìÓ¦HeaderµÈ¡£
Laravel¿ò¼ÜÌṩÁËһЩÄÚÖõÄÖÐÑë¼þ£¬ºÃ±ÈÉí·ÝÑéÖ¤ÖÐÑë¼þ¡¢CSRFÖÐÑë¼þ¡¢ÏÞÁ÷ÖÐÑë¼þµÈ£¬¿ª·¢ÕßÒ²¿ÉÒÔ×ÔÐбàд×Ô½ç˵ÖÐÑë¼þÀ´Öª×ãÓ¦ÓõÄÌض¨ÐèÇó¡£
¶þ¡¢ÖÐÑë¼þµÄʵÏÖÔÀí
Laravel¿ò¼ÜµÄÖÐÑë¼þ»úÖÆÊÇ»ùÓÚPSR-15±ê׼ʵÏֵġ£ÔÚLaravelÖУ¬ÖÐÑë¼þÒ»Ñùƽ³£°üÀ¨Á½¸ö²¿·Ö£ºÖÐÑë¼þÀàºÍÖÐÑë¼þŲÓ᣽ÓÏÂÀ´£¬ÎÒÃÇ»®·Ö¶ÔÕâÁ½²¿·Ö¾ÙÐÐÏêϸÏÈÈÝ¡£
ÖÐÑë¼þÀà
ÖÐÑë¼þÀàÊÇʵÏÖÖÐÑë¼þÂß¼µÄ½¹µã²¿·Ö£¬Ò»Ñùƽ³£ÐèҪʵÏÖPSR-15Öнç˵µÄMiddlewareInterface½Ó¿ÚÖеÄprocessÒªÁì¡£¸ÃÒªÁì½ÓÊÜÁ½¸ö²ÎÊý£¬¼´Ä¿½ñÇëÇ󹤾ߺÍÒ»¸ö»Øµ÷º¯Êý£¬»Øµ÷º¯ÊýÈÏÕ潫ÇëÇóת´ï¸øÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò¡£
ÖÐÑë¼þµÄ´¦ÀíÂß¼Ò»Ñùƽ³£ÔÚprocessÒªÁìÖÐʵÏÖ£¬¿ÉÒÔ¶ÁÈ¡ÇëÇóÊý¾Ý£¬ÐÞ¸ÄÏìÓ¦Êý¾Ý£¬ÒÔ¼°Ö´ÐÐÆäËûÌض¨µÄ²Ù×÷¡£Ê¾Àý´úÂëÈçÏ£º
<?php namespace AppHttpMiddleware; use Closure; use IlluminateHttpRequest; class MyMiddleware { /** * Handle an incoming request. * * @param IlluminateHttpRequest $request * @param Closure $next * @return mixed */ public function handle(Request $request, Closure $next) { // ÅжÏÄ¿½ñÇëÇóÊÇ·ñЯ´øtoken if (!$request->header('Authorization')) { return response()->json(['message' => 'Unauthorized'], 401); } // Ö´ÐÐÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪMyMiddlewareµÄÖÐÑë¼þÀ࣬ËüÅжÏÇëÇóÊÇ·ñЯ´øAuthorizationÍ·²¿ÐÅÏ¢£¬ÈôÊÇδЯ´ø£¬Ôò·µ»ØδÊÚȨµÄÏìÓ¦£»ÈôÊÇÒÑЯ´ø£¬ÔòÖ´ÐÐÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò¡£
ÖÐÑë¼þŲÓÃ
ÔÚLaravel¿ò¼ÜÖÐÒýÈëÖÐÑë¼þµÄ·½·¨ºÜÊÇÎÞа£¬¿ª·¢Õß¿ÉÒÔÕë¶Ô²î±ðµÄÇëÇ󷾶¡¢ÇëÇóÒªÁì¡¢ÇëÇóÀàÐ͵ÈÌõ¼þÒýÈë²î±ðµÄÖÐÑë¼þ£¬Ò²¿ÉÒÔͨ¹ýÖÐÑë¼þ×éµÄ·½·¨ÅúÁ¿ÒýÈë¡£ÏÂÃæÊǼ¸ÖÖ³£¼ûµÄÒýÈë·½·¨£º
ÒýÈëÈ«¾ÖÖÐÑë¼þ£º½«ÖÐÑë¼þÌí¼Óµ½app/Http/Kernel.phpÎļþµÄ$middlewareÊôÐÔÖУ¬Ëü½«Ó¦ÓÃÓÚËùÓеÄÇëÇó¡£
// app/Http/Kernel.php protected $middleware = [ AppHttpMiddlewareMyMiddleware::class, ];
µÇ¼ºó¸´ÖÆ
ÒýÈë·ÓÉÖÐÑë¼þ£ºÔÚroutes/web.phpÎļþÖнç˵·ÓÉʱ£¬¿ÉÒÔͨ¹ýmiddlewareÒªÁìÀ´ÒýÈëÖ¸¶¨µÄÖÐÑë¼þ¡£
// routes/web.php use AppHttpMiddlewareMyMiddleware; Route::get('/user', function () { // })->middleware(MyMiddleware::class);
µÇ¼ºó¸´ÖÆ
³ý´ËÖ®Í⣬»¹¿ÉÒÔͨ¹ýÖÐÑë¼þ×飨Middleware Groups£©µÄ·½·¨ÅúÁ¿ÒýÈë¶àÆäÖÐÑë¼þ£¬ÒÔ´ËΪ»ù´¡£¬ÎÞаӦÓÃÖÐÑë¼þ£¬ÔÚ²î±ðµÄ³¡¾°ÏÂʵÏÖ²î±ðµÄ¹¦Ð§¡£
Èý¡¢ÖÐÑë¼þÖ´ÐÐÁ÷³Ì
ÔÚÇëÇó½øÈëLaravelÓ¦ÓóÌÐòʱ£¬Laravel¿ò¼Üƾ֤ÖÐÑë¼þµÄÒýÈë˳ÐòŲÓÃÕâЩÖÐÑë¼þÀ࣬ȻºóÖ´ÐÐËüÃǵÄprocessÒªÁì¡£ÓÉÓÚÖÐÑë¼þÁ´µÄ×é³É£¬¿ÉÒÔÇáËɵØÔÚÇëÇó±»´¦Àí֮ǰ»òÖ®ºó¾ÙÐвÙ×÷¡£
Laravel¿ò¼ÜµÄÖÐÑë¼þµ÷ÀíÁ÷³ÌÈçÏ£º
ÇëÇó½øÈëÓ¦Ó㬾ÓÉHttpKernelÖÐÑë¼þ´¦Àí£¬½¨ÉèÇëÇ󹤾ߣ¨IlluminateHttpRequestʵÀý£©¡£
ŲÓÃÈ«¾ÖÖÐÑë¼þ£¬ÈôÊÇÓÐ×îÏȵÄÖÐÑë¼þÐèÒªÖ´ÐУ¬ÔòÖ´ÐиÃÖÐÑë¼þµÄprocessÒªÁì¡£
½Ó×ÅŲÓ÷ÓÉÖÐÑë¼þ£¬ÈôÊÇÓÐ×îÏȵÄÖÐÑë¼þÐèÒªÖ´ÐУ¬ÔòÖ´ÐиÃÖÐÑë¼þµÄprocessÒªÁì¡£
ÇëÇó×îÖÕµÖ´ï·Óɱհüº¯Êý»ò¿ØÖÆÆ÷ÒªÁ죬Íê³ÉÏêϸµÄÓªÒµÂß¼´¦Àí¡£
¾ÓÉ·ÓÉÖÐÑë¼þµÄprocessÒªÁì·µ»Øµ½HttpKernelÖÐÑë¼þ£¬°´Ïà·´µÄ˳ÐòŲÓ÷ÓÉÖÐÑë¼þºÍÈ«¾ÖÖÐÑë¼þµÄterminateÒªÁì¡£
×îÖÕÌìÉúHTTPÏìÓ¦£¬·µ»Ø¸øä¯ÀÀÆ÷¡£
Laravel¿ò¼ÜµÄÖÐÑë¼þÊÇÒ»¸öºÜÊÇÓÐÓõĹ¦Ð§£¬¿ÉÒÔÇáËɵØÔÚÇëÇó´¦ÀíÀú³ÌÖÐÖ´ÐÐijЩ´úÂë»ò¼ì²âÇëÇ󣬴ӶøΪӦÓóÌÐòÔöÌíÁËÎÞаÐÔºÍÇå¾²ÐÔ¡£Í¨¹ý¶ÔLaravelÖÐÑë¼þµÄÏÈÈݺÍʵÏÖÔÀíµÄÏàʶ£¬¿ª·¢Õß¿ÉÒÔ¸üÉîÈëµØÃ÷È·ºÍÓ¦ÓÃÖÐÑë¼þµÄ×÷Óá£
ÒÔÉϾÍÊÇlaravelÖÐÑë¼þµÄʵÏÖÀú³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡