2013-02-03 59 views
2


我是一個symfony新手,我試圖創建一個註冊和登錄表單。到目前爲止,我已經註冊成功。但登錄表單不斷返回「錯誤的憑據」錯誤給我。我檢查了很多有這個問題的線程,但我似乎無法弄清楚。
我創建user.php的,其中有一些基礎知識:
Symfony2登錄 - Bad Credentials |學說沒有收到任何參數

<?php 
    namespace Money\MainBundle\Entity; 

    use Doctrine\ORM\Mapping as ORM; 
    use Symfony\Component\Validator\Constraints as Assert; 
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; 
    use Symfony\Component\Security\Core\User\AdvancedUserInterface; 

    /** 
    * @ORM\Entity 
    * @ORM\Table(name="user") 
    * @UniqueEntity(fields="email", message="Email already taken", groups="registration") 
    * @UniqueEntity(fields="username", message="Username already taken", groups="registration") 
    */ 
    class User implements AdvancedUserInterface, \Serializable 
    { 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", unique=true) 
    * @Assert\NotBlank(groups="registration") 
    */ 
    private $username; 

    /** 
    * @ORM\Column(type="string", unique=true) 
    * @Assert\Email(groups="registration") 
    * @Assert\NotBlank(groups="registration") 
    */ 
    private $email; 

    /** 
    * @ORM\Column(type="string") 
    * @Assert\NotBlank(groups="registration") 
    * @Assert\MinLength(limit=4, groups="registration", message="Your password needs to have at least 4 characters") 
    */ 
    private $password; 

    /** 
    * @ORM\Column(name="is_active", type="boolean") 
    */ 
    private $isActive; 

    /** 
    * @ORM\Column(type="string", length=32) 
    */ 
    private $salt; 

    /** 
    * constructor 
    */ 

    public function __construct() 
    { 
     $this->isActive = true; 
     $this->salt = md5(uniqid(null, true)); 
    } 

    /** 
    * setters 
    */ 

    public function setEmail($email) 
    { 
     $this->email = $email; 
    } 

    public function setPassword($password) 
    { 
     $this->password = $password; 
    } 

    public function setUsername($username) 
    { 
     $this->username = $username; 
    } 

    /** 
    * advanced 
    */ 

    public function isAccountNonExpired() 
    { 
     return true; 
    } 

    public function isAccountNonLocked() 
    { 
     return true; 
    } 

    public function isCredentialsNonExpired() 
    { 
     return true; 
    } 

    public function isEnabled() 
    { 
     return $this->isActive; 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function getUsername() 
    { 
     return $this->username; 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function getEmail() 
    { 
     return $this->email; 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function getSalt() 
    { 
     return $this->salt; 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function getRoles() 
    { 
     return array('ROLE_USER'); 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function eraseCredentials() 
    { 
    } 

    /** 
    * @see \Serializable::serialize() 
    */ 
    public function serialize() 
    { 
     return serialize(array(
      $this->id, 
     )); 
    } 

    /** 
    * @see \Serializable::unserialize() 
    */ 
    public function unserialize($serialized) 
    { 
     list (
      $this->id, 
     ) = unserialize($serialized); 
    } 
} 

那麼我security.yml

jms_security_extra: 
    secure_all_services: false 
    expressions: true 

security: 
    encoders: 
     Symfony\Component\Security\Core\User\User: plaintext 
     Money\MainBundle\Entity\User: 
      algorithm: sha512 
      encode_as_base64: true 
      iterations: 10 

    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    providers: 
     user_db: 
      entity: { class: Money\MainBundle\Entity\User, property: username } 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     login: 
      pattern: ^/login$ 
      security: false 

     secured_area: 
      pattern: ^/dashboard 
      form_login: 
       check_path: /dashboard/login_check 
       login_path: /login 
      logout: 
       path: /logout 
       target:/

    access_control: 
     - { path: ^/dashboard, roles: ROLE_ADMIN } 

我創建

class LoginForm extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('username', 'text', array('attr' => array('placeholder' => 'Username', 'class' => 'span12'))); 
     $builder->add('password', 'password', array('attr' => array('placeholder' => 'Password', 'class' => 'span12'))); 
    } 

    // public function getDefaultOptions(array $options) 
    // { 
    //  return array(
    //   'data_class' => 'Money\MainBundle\Entity\User' 
    // ); 
    // } 

    public function getName() 
    { 
     return 'login'; 
    } 
} 

和登錄表單登錄操作在這裏

public function loginAction() 
    { 
     //create login form 
     $form = $this->createForm(new LoginForm(), new User()); 
     //request 
     $request = $this->getRequest(); 
     $session = $request->getSession(); 

     // get the login error if there is one 
     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(
       SecurityContext::AUTHENTICATION_ERROR 
      ); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render(
      'MoneyMainBundle:Main:signup.html.twig', 
      array(
       'error' => $error, 
       'form' => $form->createView(), 
       'register' => false 
      ) 
     ); 
    } 

也是我編碼我的密碼是這樣的:

public function signUpAction() 
{ 
    //create valid form entity 
    $form = $this->createForm(new RegisterForm(), new User()); 
    //check request 
    $request = $this->getRequest(); 
    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 

     if ($form->isValid()) { 
      //useful variables 
      $user = $form->getData(); 
      $factory = $this->get('security.encoder_factory'); 
      $encoder = $factory->getEncoder($user); 
      //encode password 
      $pass = $encoder->encodePassword($user->getPassword(), $user->getSalt()); 
      $user->setPassword($pass); 
      //save data to db 
      $em = $this->getDoctrine()->getEntityManager(); 
      $em->persist($user); 
      $em->flush(); 

      return $this->redirect($this->generateUrl('money_main_dashboard')); 
     } 
    } 

    return $this->render('MoneyMainBundle:Main:signup.html.twig', array(
     'form' => $form->createView(), 
     'register' => true, 
     'error' => false 
    )); 
} 

,最後,日誌是在這裏:

Marker - Feb 4, 2013 12:02:57 AM 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] request.INFO: Matched route "money_main_login_check" (parameters: "_route": "money_main_login_check") [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] [] 
[2013-02-04 00:03:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.email AS email3, t0.password AS password4, t0.is_active AS is_active5, t0.salt AS salt6 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] [] 
[2013-02-04 00:03:01] security.INFO: Authentication request failed: Bad credentials [] [] 
[2013-02-04 00:03:01] security.DEBUG: Redirecting to /login [] [] 
[2013-02-04 00:03:01] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest" was not called for event "kernel.request". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] security.DEBUG: Write SecurityContext in the session [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] request.INFO: Matched route "money_main_login" (parameters: "_controller": "Money\MainBundle\Controller\MainController::loginAction", "_route": "money_main_login") [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] request.INFO: Matched route "_wdt" (parameters: "_controller": "Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController::toolbarAction", "token": "510eeca5b649c", "_route": "_wdt") [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". [] [] 
[2013-02-04 00:03:01] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onKernelTerminate". [] [] 

的重要組成部分,應該是教義調試:

[2013-02-04 00:03:01] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.email AS email3, t0.password AS password4, t0.is_active AS is_active5, t0.salt AS salt6 FROM user t0 WHERE t0.username = ? LIMIT 1 ["NONE_PROVIDED"] [] 

但我不知道爲什麼它沒有收到任何後期參數

+0

你如何實現signUpAction()幫我解決類似的問題! – Aris

回答

2

問題是輸入字段中的名稱值是login [username]和login [password],我無法將這些名稱更改爲_username和_password(login_check接受),因此我不得不手動創建窗體類型和表單生成器),現在它工作。

+2

您可以在security.yml中使用這些設置來重命名您的用戶名和密碼字段:username_parameter:assword_parameter: – 10us

0

如果表單類型的類中的getName方法返回一個空字符串或null,則你的投入應該有propers名

class LoginType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder->add('_username')->add('_password', 'password'); 
    } 

    public function getName() { 
     return ''; 
    } 
} 

應該做的伎倆

0

只是把樹枝模板,並給這些字段的名稱爲'_username'和'_password'。那麼魔法應該爲你完成。

0

,您還可以在security.yml輸入以下內容,如果你創建了自己的表單類:

username_parameter: login[username] 
password_parameter: login[password]