Symfony CSRF检查失败

时间:2016-04-15 12:11:17

标签: symfony csrf

获得:

"The CSRF token is invalid. Please try to resubmit the form."

控制器:

$message = new Message();
$form = $this->createForm(new MessageType($locales), $message);
$form->handleRequest($request);

if ($form->isSubmitted()) {
    if ($form->isValid()) {
        ...
        // never reached
    }
}

检查原始POST数据,它确实包含_token字段:

message[title]: Test
message[subtitle]:
message[_token]:0LpwU3llG-FhyEc0o12b7rU0Pg2zSAb7xpUwAF1Xw3g

所以这不是很多人错过{{ form_rest(form) }}

的问题

1 个答案:

答案 0 :(得分:0)

您是否从文档(http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html)中读到了这些内容?

  

首先,配置安全组件,以便它可以使用CSRF保护。安全组件需要CSRF令牌提供程序。您可以将其设置为使用安全组件

中提供的默认提供程序
# app/config/security.yml 
security: 
  # ... 
  firewalls: 
    secured_area: 
      # ... 
      form_login: 
        # ... 
        csrf_token_generator: security.csrf.token_manager

我想你可能需要指定哪个令牌管理器用于_token

相关问题