2015-05-22 101 views
0

我想有兩種不同的(我指的是不同的模板)表單,可以在不同的樹枝中登錄我的用戶。但我從我的一個表單中得到一個錯誤...這就是我一步一步做的:Symfony2 FOSUser/SonataBundle無效的CSRF令牌值

我有路線/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 <[email protected]> 
* 
* 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 %} 

The ful l錯誤:如果我嘗試登錄/login_frm,我得到Invalid CSRF token.錯誤。如果我從/login登錄,它工作正常。這是爲什麼?不能我有兩種形式登錄?

+0

[CSRF令牌無效。請嘗試重新提交表單](http://stackoverflow.com/questions/23455780/the-csrf-token-is-invalid-please-try-to-resubmit-the-form) –

+0

它不是重複的,ty: ) – Dominykas55

回答

0

因爲在標準格式中您有CSRF令牌,而您的自定義格式中沒有。

你應該在你的HTML的表格塊中加入{{ form_widget(form._token) }}

+0

他有 「」這對fosuser很好 – Koalabaerchen

+0

這是正確的,我有一個輸入。 – Dominykas55

+0

input with名稱爲_csrf_token,但表單名稱又如何?試試我的解決方案我認爲它會產生另一個名字的輸入。 –