【问题标题】:Zend Framework 2 CSRF validationZend Framework 2 CSRF 验证
【发布时间】:2017-07-09 11:54:09
【问题描述】:

我有基于 Zend 框架 2 的应用程序。我有一个带有 CSRF 字段的表单。如果我填写表格并在大约 5 分钟后提交,它会给我The form submitted did not originate from the expected site 验证错误。

所以我认为这可能是会话配置的一些问题。然后我在module.config.php 上将SessionConfig 的选项添加为follows

'session' => array(
    'remember_me_seconds' => 2419200,
    'use_cookies' => true,
    'cookie_httponly' => true,
    'cookie_lifetime' => '2419200',
    'gc_maxlifetime' => '2419200'
),

但问题依然存在。你知道如何解决这个问题吗?

--更新--

我的表单类包含如下CSRF元素,

    $this->add(array(
        'type' => 'Zend\Form\Element\Csrf',
        'name' => 'security',
        'options' => array(
            'csrf_options' => array(
                'timeout' => 20000
            )
        )
    ));

这些似乎都不起作用。

【问题讨论】:

    标签: php zend-framework2 csrf


    【解决方案1】:

    ZendFramework 下的 Csrf 系统根据 timeout 键下 Csrf 元素的配置中存储的参数配置会话持续时间,如下例所示:

    $form->add([
        'type' => Element\Csrf::class,
        'name' => 'csrf',
        'options' => [
            'csrf_options' => [
                'timeout' => 600,
            ],
        ],
    ]);
    

    将会话配置放在config 键下,如下例所示:

    'session' => [
        'config' => [
            'class'   => Zend\Session\Config\SessionConfig::class,
            'options' => [
                'name'                => 'SID',
                'use_cookies'         => true,
                'cookie_httponly'     => true,
                'remember_me_seconds' => 2419200,
            ],
        ],
    ]
    

    【讨论】:

    • 事实上我已经这样做了。我会更新这个问题。即使超时,它也有问题。
    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    相关资源
    最近更新 更多