【问题标题】:Which is the better way to setup CSRF protection in Laravel?在 Laravel 中设置 CSRF 保护的更好方法是什么?
【发布时间】:2015-02-19 14:18:37
【问题描述】:

将此添加到 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'));

哪种方法更好,为什么?

【问题讨论】:

    标签: php laravel csrf


    【解决方案1】:

    两者的效果相同,但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'));
    

    【讨论】:

    • 请注意,这可能适用于身份验证过滤器。
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2019-06-27
    • 2016-04-02
    • 2012-05-01
    • 2015-05-22
    相关资源
    最近更新 更多