CakePHP 3.6.10 完全禁用 CSRF 令牌检查

CakePHP 3.6.10 disable completely CSRF token check

我需要为我的应用程序完全禁用 CSRF 令牌的控制。 我尝试使用:

    public function beforeFilter(Event $event)
    {
      $this->getEventManager()->off($this->Csrf);
    }

在 AppController 中,但它似乎不起作用。 手动 link:Disabling the CSRF Component for Specific Actions

我做了很多测试,看了很多帖子都解决不了。

Ty.

@omerowitz 这是过滤操作之前我的 AppController:

    public function beforeFilter(Event $event)
{
    $this->getEventManager()->off($this->Security);
    if($this->request->is('post')) {
        $this->getEventManager()->off($this->Csrf);
    }
    $this->Auth->allow(['index', 'view', 'display']);
}

但是还是不行,当我用邮递员

发出请求时,我仍然有错误'CSRF token mismatch.'

解决方案:

我已经删除了这个:

->add(new CsrfProtectionMiddleware([
     'httpOnly' => true
  ]));

来自 Application.php。 为什么手册中没有说明?

全部!

您还需要禁用 Security 组件。我将它用于我的 API 控制器:

$this->getEventManager()->off($this->Security);

if($this->request->is('post')) {
    $this->getEventManager()->off($this->Csrf);
}

我只针对 POST 请求禁用它,尽管同时禁用 SecurityCsrf 也可以。


编辑:我把它放在我的 AppController 中,尽管它会在每个控制器上工作。

安全组件似乎启用了 CSRF 和表单篡改。

https://book.cakephp.org/3.0/en/controllers/components/security.html

你可以试试这个

 public function beforeFilter(Event $event)
{
  $this->getEventManager()->makeMess($this->Csrf);
}

对我有用!

您也可以尝试使用 Python 语言或 Symfony 2.8。

我认为在 Cake 3.6 中你应该从中间件队列中删除 CsrfProtectionMiddlewaresrc/Application.php

//Src/Application.php

public function middleware($middlewareQueue)
{
    $middlewareQueue
        // Catch any exceptions in the lower layers,
        // and make an error page/response
        ->add(ErrorHandlerMiddleware::class)

        // Handle plugin/theme assets like CakePHP normally does.
        ->add(new AssetMiddleware([
            'cacheTime' => Configure::read('Asset.cacheTime')
        ]))

        // Add routing middleware.
        // Routes collection cache enabled by default, to disable route caching
        // pass null as cacheConfig, example: `new RoutingMiddleware($this)`
        // you might want to disable this cache in case your routing is extremely simple
        ->add(new RoutingMiddleware($this, '_cake_routes_'));

        // Add csrf middleware.
        //Comment following Code.
       /* ->add(new CsrfProtectionMiddleware([
            'httpOnly' => true
        ]));*/

    return $middlewareQueue;
}

//在我的例子中你的特定控制器 //用户控制器:

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->viewBuilder()->layout('admin');
    $this->getEventManager()->off($this->Security);        
}

// 用于初始化方法

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Security');
}

试试这个它的工作...

在 CakePHP 3.6.10 中:

  1. 转到 src/Application。php
  2. 搜索功能中间件
  3. 评论下面一行:

    ->添加(新的 CsrfProtectionMiddleware([ 'httpOnly' => 真 ]));

这将完全禁用 CSRF 令牌检查。

我正在使用 whitelistCallback 作为特殊前缀或动作数组

// in src/Application.php
use Cake\Http\Middleware\CsrfProtectionMiddleware;

public function middleware($middlewareQueue) {
    $csrf = new CsrfProtectionMiddleware();

    // Token check will be skipped when callback returns `true`.
    $csrf->whitelistCallback(function ($request) {
        // Skip token check for API URLs.
        if ($request->getParam('prefix') === 'api') {
            return true;
        }
    });

    // Ensure routing middleware is added to the queue before CSRF protection middleware.
    $middlewareQueue->add($csrf);

    return $middlewareQueue;
}