如何在 Zend Framework 3 中实现 CSRF 保护



我需要在 Zend Framework 3 中实现针对 CSRF 攻击的保护。我读出了文档,但无法弄清楚它在 ZF3 的哪些部分被描述,我只能为 ZF2 获得它。请帮忙。

在表单中添加CSRF,它类似于添加任何FORM ELEMENTCSRF是隐藏输入。

<?php
namespace YOUR_MODULEForm;

use ZendFormForm;
use ZendFormElement;
class AnimationCategoryForm extends Form
{
    public function __construct($name = null, array $options = [])
    {
        parent::__construct($name, $options);
    }
    public function init()
    {
        $this->add([
            'type' => ElementCsrf::class,
            'name' => 'csrf',
            'options' => [
            'csrf_options' => [
               'timeout' => 600,
            ],
        ],
     ]);
        $this->add([
            'type' => ElementSubmit::class,
            'name' => 'submit',
            'attributes' => [
                'value' => 'Submit',
            ],
        ]);
    }
}

以上是表单示例。您还应该在视图文件中显示它。

最新更新