[英]Which is the better way to setup CSRF protection in Laravel?
將其添加到BaseController.php:
public function __construct() {
// Run the 'csrf' filter on all post, put, patch and delete requests.
$this->beforeFilter('csrf', ['on' => ['post', 'put', 'patch', 'delete']]);
}
或者將其添加到routes.php:
Route::when('*', 'csrf', array('post', 'put', 'patch', 'delete'));
哪種方式更好,為什么?
兩者都有相同的效果,但Router::when
方法似乎是優先的。
在沒有正確的parent::__construct()
調用的情況下,擴展錯誤的控制器或重載BaseController::__construct()
非常容易。 在這兩種情況下都不會發生錯誤。 如果這種情況偶然發生,你會有一個無聲的安全漏洞:
class FooController extends App\BaseController
{
public function __construct()
{
$this->initializeSomething()
// somebody forgot to call parent::__construct()
}
public function action()
{
// no CSRF protection here!
}
}
使用路由器似乎不易出錯,以后沒有簡單的方法來意外覆蓋過濾器。
Route::when('*', 'csrf', array('post', 'put', 'patch', 'delete'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.