fko*_*ler 16 login csrf symfony fosuserbundle symfony-2.0
我正在使用Symfony2.0和FOSUserBundle,并希望在我的登录表单上禁用csrf令牌.
我已经在我的config.yml网站上全局禁用了csrf保护:
framework:
csrf_protection:
enabled: false
Run Code Online (Sandbox Code Playgroud)
这很好用,我的表单中没有添加csrf字段.但是,这不适用于登录表单.仅在此表单上,如果我在表单中不包含令牌,则会收到"无效的CSRF令牌"错误:
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />
Run Code Online (Sandbox Code Playgroud)
如何在登录表单上禁用CSRF令牌?
Jua*_*osa 29
您可以通过'csrf_protection' => false在其options数组中进行设置来禁用表单类中的CSRF保护:
class LoginType extends AbstractType
{
// ...
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\UserBundle\Entity\User',
'csrf_protection' => false
);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果您使用FormBuilder创建表单而不是AbstractType类,则可以将options数组作为第二个参数传递,createFormBuilder()如下所示:
$form = $this->createFormBuilder($users, array('csrf_protection' => false))
->add( ... )
->getForm();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21903 次 |
| 最近记录: |