laravel 5.7»á¼û²»ÁË·ÓÉ
Ëæ×Å laravel 5.7 µÄÍƳö£¬ÓÐÐí¶àÓû§·´Ó¦»á¼û·ÓÉ·ºÆðÁËÎÊÌâ¡£Õâ¸öÎÊÌâµÄ·ºÆð¿ÉÄÜ»áÈÃÄãÔÚ¿ª·¢Àú³ÌÖÐÆÌÕÅ´ó×ÚµÄʱ¼äÀ´´¦Öóͷ£¡£ÎªÁË×ÊÖú¸÷È˽â¾öÕâ¸öÎÊÌ⣬±¾ÎĽ«ÏêϸÏÈÈÝ laravel 5.7 »á¼û²»ÁË·ÓɵĽâ¾ö¼Æ»®¡£
Ê×ÏÈÎÒÃÇÐèÒªÏàʶ Laravel 5.7 µÄһЩÐÂת±ä£¬ÕâЩת±ä¿ÉÄܻᵼÖÂ֮ǰµÄ´úÂëÎÞ·¨Õý³£ÔËÐС£ÆäÖÐ×îÖ÷ÒªµÄÒ»¸öת±äÊÇĬÈÏÌí¼ÓÁË CSRF ±£»¤ÖÐÐļþ¡£ÕâÆäÖÐÐļþ¿ÉÒÔÈ·±£Óû§Ìá½»µÄ±íµ¥Êý¾ÝÊÇ·ñÀ´×ÔÓÚÄãµÄÓ¦ÓóÌÐò£¬±ÜÃâÓ¦ÓóÌÐòÔâÊÜ CSRF ¹¥»÷¡£
CSRF ±£»¤ÖÐÐļþÔÚ Laravel 5.6 ÖÐÒѾ±£´æ£¬µ«Ä¬Èϲ¢Ã»ÓÐÆôÓᣵ«ÔÚ Laravel 5.7 ÖУ¬Laravel ÍŶÓĬÈÏÆôÓÃÁËÕâÆäÖÐÐļþ¡£Òò´Ë£¬ÔÚ Laravel 5.7 ÖУ¬Ê¹ÓÃ±íµ¥Ìá½»Êý¾ÝʱÐèÒªÌí¼Ó CSRF ÁîÅÆ¡£
½â¾öÒªÁ죺
ÓÐÁ½ÖÖÒªÁì¿ÉÒÔ½â¾ö Laravel 5.7 »á¼û²»ÁË·ÓɵÄÎÊÌâ¡£µÚÒ»ÖÖÒªÁìÊÇÔÚ web.php ÎļþÖÐÌí¼Ó CSRF ÁîÅÆ¡£µÚ¶þÖÖÒªÁìÊǽûÓà CSRF ±£»¤ÖÐÐļþ¡£
ÒªÁìÒ»£ºÔÚ web.php ÎļþÖÐÌí¼Ó CSRF ÁîÅÆ
Laravel 5.7 ´úÂëµÄ RouteServiceProvider.php ÖÐĬÈÏʹÓÃÁË web ÖÐÐļþ×飬Òò´Ë£¬ÔÚ web.php ÎļþÖÐÌí¼Ó CSRF ÁîÅÆ×î¼òÆÓµÄÒªÁì¾ÍÊÇʹÓà {{ csrf_field() }} ÒªÁì¡£
ÀýÈ磬ÈôÊÇÄãÓÐÒ»¸ö´¦Öóͷ£ POST ÇëÇóµÄ·ÓÉ£º
Route::post(‘/foo’, function () {
return 'Hello World';
µÇ¼ºó¸´ÖÆ
});
ÄãÐèÒªÔÚ±íµ¥ÖÐÌí¼Ó {{ csrf_field() }} ÒªÁìÀ´ÌìÉú CSRF ÁîÅÆ£º
{{ csrf_field() }} <!-- ... -->
µÇ¼ºó¸´ÖÆ
Õâ¸öÒªÁì¿ÉÒÔÈ·±£ÄãµÄ Laravel 5.7 Ó¦ÓóÌÐò¿ÉÒÔÕý³£´¦Öóͷ£ POST ÇëÇó¡£
ÒªÁì¶þ£º½ûÓà CSRF ±£»¤ÖÐÐļþ
½ûÓà CSRF ±£»¤ÖÐÐļþÊÇÁíÒ»¸ö½â¾ö Laravel 5.7 »á¼û²»ÁË·ÓɵÄÒªÁì¡£ËäÈ»ÕâÖÖÒªÁì¿ÉÄÜ»áÉÔ΢½µµÍÄãµÄ Laravel Ó¦ÓóÌÐòµÄÇå¾²ÐÔ£¬µ«ÈôÊÇÄãµÄÓ¦ÓóÌÐò²»»áÊܵ½ CSRF ¹¥»÷µÄÍþв£¬ÄÇôÕâÖÖÒªÁìÊÇ¿ÉÐеġ£
Òª½ûÓà CSRF ±£»¤ÖÐÐļþ£¬ÄãÐèÒªÔÚ VerifyCsrfToken ÀàµÄ $except ÊôÐÔÖÐÌí¼ÓÒªºöÂ﵀ URI¡£
·¿ª app/Http/Middleware/VerifyCsrfToken.php Îļþ£¬È»ºóÔÚ $except ÊôÐÔÖÐÌí¼ÓÒªºöÂԵķÓÉ£º
protected $except = [
'your-route-to-ignore'
µÇ¼ºó¸´ÖÆ
];
ÀýÈ磬¼ÙÉèÄãÏëºöÂÔ¡°register¡±Â·ÓÉ£º
protected $except = [
'register'
µÇ¼ºó¸´ÖÆ
];
Õâ¸öÒªÁì¿ÉÒÔÈ·±£ÄãµÄ Laravel 5.7 Ó¦ÓóÌÐò¿ÉÒÔÕý³£´¦Öóͷ£ËùÓÐÇëÇó¡£
½áÂÛ£º
×ܵÄÀ´Ëµ£¬ÔÚ Laravel 5.7 Öлá¼û²»ÁË·ÓɵÄÎÊÌâÖ÷ÒªÊÇÓÉÓÚĬÈÏÆôÓÃÁË CSRF ±£»¤ÖÐÐļþËùµ¼Öµġ£Òò´Ë£¬ÈôÊÇÄãÔÚ¿ª·¢µÄÀú³ÌÖÐÓöµ½ÁËÕâ¸öÎÊÌ⣬¿ÉÒÔʵÑéʹÓÃÕâÁ½ÖÖÒªÁìÀ´½â¾ö¡£ÈôÊÇÄãÒª½ûÓà CSRF ±£»¤ÖÐÐļþ£¬Ó¦¸Ã½ö¶ÔÄÇЩ²»Éæ¼°Ãô¸Ð»òÖ÷ÒªÊý¾ÝµÄ·ÓÉʹÓá£Õâ¸öÒªÁì¿ÉÄÜ»áÂÔ΢½µµÍÄãµÄ Laravel Ó¦ÓóÌÐòµÄÇå¾²ÐÔ£¬Òò´Ë½¨ÒéʹÓÃÒªÁìÒ»À´È·±£ÄãµÄÓ¦ÓóÌÐòµÄÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇlaravel 5.7»á¼û²»ÁË·ÓɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡