使用sha512编码密码在Symfony2中无法正常工作

时间:2014-12-01 10:32:22

标签: database symfony passwords encoder sha512

我使用sha512作为密码编码器。虽然密码不使用这种编码器记录在数据库中。例如,如果密码为“123”,则将其哈希(使用sha512):

3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2

但是,实际上密码存储在我拥有的数据库中:iOgyhdY1gJJPj7y7mMN8obgqMQZH2fLDuQuXfqZesC1Iqxo6iHxRuAA9m8E1ZUz76OIiPGTann7uJ3BNhPDoEA==

这是我拥有的文件security.yml的代码:

security:
firewalls:
    secured_area:

        pattern:    ^/
        anonymous: ~
        form_login:
            login_path:  /login
            check_path:  /login/check
            default_target_path: /home
            always_use_default_target_path: true
        logout:
            path:   /home/logout
            target: /login
        remember_me:
            key:      %secret%
            lifetime: 604800 
            path:     /
            domain:   ~


access_control:
    - { path: ^(?!/login), role: IS_AUTHENTICATED_FULLY }

providers:
    main:
        entity: { class: Ikproj\LoginBundle\Entity\User, property: username }

encoders:
    Ikproj\LoginBundle\Entity\User: sha512

这是User.php文件的代码:

   <?php

namespace Ikproj\LoginBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="Ikproj\LoginBundle\Entity\UserRepository")
 */
class User implements UserInterface
{  
    /**
     * @var integer
     *
     * @ORM\Column(name="id_user", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=255)
     */
    private $username;

    /**
     * @var string
     *
     * @ORM\Column(name="pseudo", type="string", length=255)
     */
    private $pseudo;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=255)
     */
    private $password;

    /**
     * @var string
     *
     * @ORM\Column(name="sexeuser", type="string", length=255)
     */
    private $sexeuser;

    /**
     * @var \Date
     *
     * @ORM\Column(name="dateanniv", type="date")
     */
    private $dateanniv;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

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

    /**
     * Set pseudo
     *
     * @param string $pseudo
     * @return User
     */
    public function setPseudo($pseudo)
    {
        $this->pseudo = $pseudo;

        return $this;
    }

    /**
     * Get pseudo
     *
     * @return string 
     */
    public function getPseudo()
    {
        return $this->pseudo;
    }

    /**
     * Set passWD
     *
     * @param string $passWD
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set sexeuser
     *
     * @param string $sexeuser
     * @return User
     */
    public function setSexeuser($sexeuser)
    {
        $this->sexeuser = $sexeuser;

        return $this;
    }

    /**
     * Get sexeuser
     *
     * @return string 
     */
    public function getSexeuser()
    {
        return $this->sexeuser;
    }

    /**
     * Set dateanniv
     *
     * @param \DateTime $dateanniv
     * @return User
     */
    public function setDateanniv($dateanniv)
    {
        $this->dateanniv = $dateanniv;

        return $this;
    }

    /**
     * Get dateanniv
     *
     * @return \DateTime 
     */
    public function getDateanniv()
    {
        return $this->dateanniv;
    }

    public function getRoles()
    {
        return array('ROLE_ADMIN');
    }

    public function getSalt()
    {
        return null;
    }

    public function eraseCredentials()
    {

    }

    public function equals(UserInterface $user)
    {
        return $user->getUsername() == $this->getUsername();
    }   
}

当我使用密码“123”登录时,它没有任何问题。我不知道它是如何接受密码的,虽然它没有以正确的形式编码!! 所以我的问题是:

  1. 为什么密码“123”以这种形式存储在数据库中?
  2. 用于获取以下结果的编码方法是什么:iOgyhdY1gJJPj7y7mMN8obgqMQZH2fLDuQuXfqZesC1Iqxo6iHxRuAA9m8E1ZUz76OIiPGTann7uJ3BNhPDoEA==
  3. 我的代码有什么问题?

2 个答案:

答案 0 :(得分:2)

默认情况下,选择算法时,默认选项是使用此算法迭代5000次,然后对结果执行base64编码。这应该解释结果字符串。

至于您的代码有什么问题,我不确定您的实际问题是什么?

答案 1 :(得分:0)

尝试只迭代一次并禁用base64_encoding

尝试:security: encoders: Symfony\Component\Security\Core\User\User: algorithm: sha512 encode_as_base64: false iterations: 1