首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在AJAX中使用Zend Framework表单Hash (token)

如何在AJAX中使用Zend Framework表单Hash (token)
EN

Stack Overflow用户
提问于 2010-03-19 11:18:39
回答 4查看 11.7K关注 0票数 17

我已经将Zend_Form_Element_Hash包含到一个表单多选项框表单中。我将jQuery设置为在单击复选框时触发AJAX请求,并将令牌与此AJAX请求一起传递。第一个AJAX请求运行良好,但随后的请求失败。

我怀疑一旦令牌通过验证,它就会从会话中删除(hop = 1)。

在使用Zend Framework Hash保护表单的同时,使用AJAX来完成其中的一些请求,您的攻击计划是什么?

EN

回答 4

Stack Overflow用户

发布于 2011-03-11 09:21:32

这就是如何在ajax表单中处理散列字段:

代码语言:javascript
复制
class AuthController extends Zend_Controller_Action
{
    public function init()
    {
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
        $contextSwitch->addActionContext('index', 'json')
                      ->initContext();
    }

    public function loginAction()
    {
        $form = new Application_Form_Login();
        $request = $this->getRequest();

        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                // some code ..
            } else {
                // some code ..

                // Regenerate the hash and assign to the view
                $reservationForm->hash->initCsrfToken();
                $this->view->hash = $reservationForm->hash->getValue();
            }
        }
        $this->view->form = $form;
    }
}

然后在视图脚本中..

代码语言:javascript
复制
<? $this->dojo()->enable()
                ->requireModule('dojox.json.query')
                ->onLoadCaptureStart() ?>
function() {
    var form = dojo.byId("login_form")
    dojo.connect(form, "onsubmit", function(event) {
        dojo.stopEvent(event);

        var xhrArgs = {
            form: this,
            handleAs: "json",
            load: function(data) {
                // assign the new hash to the field
                dojo.byId("hash").value = dojox.json.query("$.hash", data);

                // some code ..
            },
            error: function(error) {
                // some code ..
            }
        }
        var deferred = dojo.xhrPost(xhrArgs);
    });
}
<? $this->dojo()->onLoadCaptureEnd() ?>

希望还不算太晚:D

票数 8
EN

Stack Overflow用户

发布于 2010-04-01 12:30:13

有一个解决方案:

除了包含数据的表单之外,还要创建一个没有元素的表单。您可以从控制器实例化这两个表单。同样在控制器中,您可以将元素散列添加到空表单。这两种形式都应该发送给视觉。然后,在控制器中的条件"if ($ request-> isXmlHttpRequest ())“中呈现空表单。然后,使用"getValue ()“方法获取散列值。这个值必须由Ajax作为响应发送,然后使用JavaScript替换已经过时的散列值。为散列创建空表单的选项是为了避免诸如验证码之类的其他元素出现问题,如果该表单被呈现,则将再次生成其id,并且还将需要替换新信息。验证将单独完成,因为有两个不同的表单。稍后,您可以随时重用散列(空)表单。以下是这些代码的示例。

代码语言:javascript
复制
//In the controller, after instantiating the empty form you add the Hash element to it:
$hash = new Zend_Form_Element_Hash('no_csrf_foo');
$hash_form->addElement('hash', 'no_csrf_foo', array('salt' => 'unique'));

 //...

//Also in the controller, within the condition "if ($request->isXmlHttpRequest())" you render the form (this will renew the session for the next attempt to send the form) and get the new id value:
$hash_form->render($this->view);
$hash_value['hash'] = $hash_form->getElement('no_csrf_foo')->getValue();//The value must be added to the ajax response in JSON, for example. One can use the methods Zend_Json::decode($response) and Zend_Json::encode($array) for conversions between PHP array and JSON.

//---------------------------------------

//In JavaScript, the Ajax response function:
document.getElementById("no_csrf_foo").value = data.hash;//Retrieves the hash value from the Json response and set it to the hash input.

狮子座

票数 4
EN

Stack Overflow用户

发布于 2010-03-21 23:51:53

形式哈希在原则上是很棒的,但在实践中却是一场噩梦。我认为处理这个问题的最好方法是在您发出请求时将新的散列与响应一起返回,并根据需要更新表单标记或将其存储在javascript的内存中。

新的散列可以从表单对象中获得,也可以从会话中读取。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2474774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档