Symfony2 FOSUser / SonataBundle无效的CSRF令牌值

时间:2015-05-22 11:05:05

标签: php forms symfony login fosuserbundle

我希望有两种不同的(不同的是我的意思是模板)表单,可以在不同的树枝上登录我的用户。但我从我的一个表格中得到错误......这就是我一步一步做的:

我在路由/login上有主登录模板。 此登录有效!

要使第二个表单在/product_frm中起作用,我就这样做了:

1)为第二个登录控制器创建路由。所以现在我有两条登录路线:

login:  
    path: /login
    defaults: { _controller: ApplicationSonataUserBundle:SecurityFOSUser1:login }

loginForm:
    path: /login_frm
    defaults: { _controller: ApplicationSonataUserBundle:LoginFormType:login }

现在这是/login_frm 的控制器,与/login控制器相同,只有渲染的模板不同

<?php
/*
 * This file is part of the Sonata package.
 *
 * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace Application\Sonata\UserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController;
use Sonata\UserBundle\Model\UserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Sonata\UserBundle\Controller\SecurityFOSUser1Controller as BaseController;


class LoginFormTypeController extends BaseController
{
    public function loginAction()
    {
        $user = $this->container->get('security.context')->getToken()->getUser();

        if ($user instanceof UserInterface) {
            $this->container->get('session')->getFlashBag()->set('sonata_user_error', 'sonata_user_already_authenticated');
            $url = $this->container->get('router')->generate('sonata_user_profile_show');

            return new RedirectResponse($url);
        }

        return parent::loginAction();
    }
    public function renderLogin(array $data)
    {
        $template = sprintf('MpShopBundle:Form:login_form2.html.%s', $this->container->getParameter('fos_user.template.engine'));

        return $this->container->get('templating')->renderResponse($template, $data);
    }

}

最后,我制作了加载表单的模板:

{% block fos_user_content %}

    <div class="span9">
        <div class="well">
            {% block sonata_user_login %}
                <div class="panel panel-info">

                <div class="panel-heading">
                    <h2 class="panel-title">{{ 'title_user_authentication'|trans({}, 'SonataUserBundle') }}</h2>
                </div>

                <div class="panel-body">

                    {% block sonata_user_login_error %}
                        {% if error %}
                            <div class="alert alert-danger alert-error">{{ error|trans({}, 'FOSUserBundle') }}</div>
                        {% endif %}
                    {% endblock %}

                    {% block sonata_user_login_form %}
                        <form action="{{ path("fos_user_security_check") }}" method="post" role="form"
                              class="form-horizontal">
                            <input type="hidden" name="_csrf_token" value="{{ csrf_token }}"/>

                            <div class="control-group">
                                <label class="control-label control-label required" for="username"
                                       class="col-sm-4 control-label">{{ 'security.login.username'|trans({}, 'SonataUserBundle') }}</label>

                                <div class="controls">

                                <input type="text" class="form-control" id="username" name="_username" value="{{ last_username }}" required="required"/></div>
                            </div>


                            <div class="form-group control-group">
                                <label class="control-label control-label required" for="password"
                                       class="col-sm-4 control-label">{{ 'security.login.password'|trans({}, 'SonataUserBundle') }}</label>

                                <div class="controls">
                                <input type="password" class="form-control" id="password" name="_password" required="required"/></div>
                            </div>

                            <div class="control-group">
                                <div class="col-sm-offset-4 col-sm-8">
                                    <div class="checkbox control-group">
                                        <label class="checkbox pull-left" for="remember_me">
                                            <input type="checkbox" id="remember_me" name="_remember_me" value="on"/>
                                            {{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <div class=" control-group pull-left">
                                <div class="">
                                    <a href="{{ path('forgetpass') }}">{{ 'forgotten_password'|trans({}, 'SonataUserBundle') }}</a>
                                </div>
                            </div>

                            <div class="form-actions">
                                <div class="pull-left">
                                    <input type="submit" id="_submit" name="_submit" class="btn btn-primary pull-right"
                                           value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}"/>
                                </div>
                            </div>
                        </form>
                    {% endblock %}
                </div>
            </div>
            {% endblock %}
        </div>
       </div>

{% endblock fos_user_content %}

完整错误:如果我尝试登录/login_frm,则会收到Invalid CSRF token.错误。如果我从/login登录,它可以正常工作。这是为什么?我有两种表格可以登录吗?

1 个答案:

答案 0 :(得分:0)

因为在标准格式中您拥有CSRF令牌,并且您的自定义表单中没有。

您应该在HTML的表单块中添加{{ form_widget(form._token) }}