我是symfony2和doctrine的新手.我正在尝试基于文档和一些教程的身份验证系统.我不知道为什么我这么难找到它.
这是我基于2个单独的教程做的.
我的用户实体:
isActive = true; $this->salt = md5(uniqid(null, true)); } /** * @inheritDoc */ public function getUsername() { return $this->username; } /** * @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, $this->username, $this->salt, $this->password, )); } /** * @see \Serializable::unserialize() */ public function unserialize($serialized) { list ( $this->id, $this->username, $this->salt, $this->password, ) = unserialize($serialized); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set username * * @param string $username * @return User */ public function setUsername($username) { $this->username = $username; return $this; } /** * Set salt * * @param string $salt * @return User */ public function setSalt($salt) { $this->salt = $salt; return $this; } /** * Set password * * @param string $password * @return User */ public function setPassword($password) { $this->password = $password; return $this; } /** * Set email * * @param string $email * @return User */ public function setEmail($email) { $this->email = $email; return $this; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } /** * Set isActive * * @param boolean $isActive * @return User */ public function setIsActive($isActive) { $this->isActive = $isActive; return $this; } /** * Get isActive * * @return boolean */ public function getIsActive() { return $this->isActive; } }
我的security.yml
# app/config/security.yml security: encoders: Login\LoginBundle\Entity\User: algorithm: sha512 encode_as_base64: true iterations: 1 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ] providers: administrators: entity: { class: LoginLoginBundle:User, property: username } firewalls: admin_area: pattern: ^/ anonymous: ~ form_login: login_path: login check_path: login_check logout: path: /logout target: /login access_control: - { path: ^/admin, roles: ROLE_ADMIN }
我的控制器
setUsername('aa'); $user->setEmail('admin@umahanov.com'); $user->setSalt(md5(time())); $user->setIsActive(false); $encoder = new MessageDigestPasswordEncoder('sha512',true,1); $password = $encoder->encodePassword('1234', $user->getSalt()); $user->setPassword($password); $manager = $this->getDoctrine()->getManager(); $manager->persist($user); $manager->flush(); return $this->render('LoginLoginBundle:Default:index.html.twig', array('name' => $name)); } }
所以想法是在/hello/me
被叫时插入用户.并在admin/hello/me
调用时执行登录.现在,在第一次迭代后,我的数据库包含此数据
1 d033e22ae348aeb5660fc2140aec35850c4da997 admin admin@example.com 1
但是,当我提供aa
并1234
以登录形式作为凭证时,它显示*错误的凭据.我错过了什么?*是否有任何简单的教程来简单地使用symfony2描述整个身份验证.还有简单的密码.请帮助......请:(
您正在使用Sha512生成128个字符哈希.密码的数据库存储是40个字符.
尝试使用Sha1或其他散列算法生成少于40个字符散列.或者增加该字段的存储大小.