FOSRestBundle&无效的表单CRF令牌

时间:2016-04-05 08:06:00

标签: symfony fosrestbundle

我正在尝试实现FOSRestBundle和Symfony表单。 我找到了此tutorial,但我对this部分

有疑问
private function processForm(PageInterface $page, array $parameters, $method = "PUT")
{
    $form = $this->formFactory->create(new PageType(), $page, array('method' => $method));
    $form->submit($parameters, 'PATCH' !== $method);
    if ($form->isValid()) { //form is not valid.
        $page = $form->getData();
        $this->om->persist($page);
        $this->om->flush($page);
        return $page;
    }
    throw new InvalidFormException('Invalid submitted data', $form);
}
  

错误:CSRF令牌无效。请尝试重新提交表单。

Here是教程中的控制器。这是我的班长:

public function newAction(Request $request)
{

    $form = new EntryType();
    $newEntry = $this->container->get('entries.entry.handler')->post(
        $request->request->get($form->getName())
    );

    return View::create()
        ->setStatusCode(200)
        ->setFormat('json')
        ->setSerializationContext(SerializationContext::create()->setGroups(array('list')))
        ->setData($newEntry);
}

我应该跳过检查isValid()还是以某种方式修复此问题?怎么样?

好的,现在很清楚了。应禁用CRF验证(csrf_protection)

CSRF token is invalid when calling rest post api from php Client https://github.com/liuggio/symfony2-rest-api-the-best-2013-way/issues/1#issuecomment-31435232 CSRF validation needed or not when using RESTful API?

1 个答案:

答案 0 :(得分:4)

tutorial的第3部分:

  

可以根据用户的角色禁用CSRF。

# app/config/config.yml
fos_rest:
    disable_csrf_role: ROLE_API
    # you can also try
    # disable_csrf_role: IS_AUTHENTICATED_FULLY

另见issue