【问题标题】:csrf_token symfony2 validatecsrf_token symfony2 验证
【发布时间】:2014-04-28 09:31:06
【问题描述】:

我是 symfony2 的新手,当我尝试以自定义表单验证 csrf 令牌时遇到问题,我的问题是“如何在控制器中验证 csrf 令牌?

在我看来,这是我的代码。

<form role="form" action="{{ path('default_select_city_check') }}" method="post">
       <input type="hidden" name="_csrf_token" value="{{ csrf_token('default_select_city_check') }}">
            ...
        </form>

这是我在控制器中的代码:

public function selectCityCheckAction(Request $request) {
    // in this part, how can I compare the token value in the form with the token value in the session?
}

感谢您的帮助

【问题讨论】:

    标签: forms validation symfony token csrf


    【解决方案1】:

    在你的控制器中添加一个函数:

     public function isCsrfTokenValid($intention, $token_name = '_csrf_token')
     {
        return $this->get('form.csrf_provider')
            ->isCsrfTokenValid($intention, $this->getRequest()->get($token_name));
     }
    

    在你的行动中:

    if ($this->isCsrfTokenValid('default_select_city_check')) {
       //do something you want
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-05
      • 2016-06-01
      • 2012-04-28
      • 2015-05-16
      • 2013-01-21
      • 1970-01-01
      • 2012-10-21
      • 2011-12-27
      相关资源
      最近更新 更多