如何从数据库实现身份验证

时间:2014-05-12 13:29:54

标签: symfony authentication

我已经尝试了好几个小时但却无法让它发挥作用。采用Acme DemoBundle并添加了一个名为User的实体:

的Symfony / SRC / Acme公司/ DemoBundle /实体/ user.php的

内容:

<?php
namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

$fp = fopen('/var/www/html/hosts/Acme/Symfony/app/logs/data.txt', 'w');
fwrite($fp, 'file is included');


/**
 * Acme\DemoBundle\Entity\User
 *
 * @ORM\Table(name="jos_users")
 * @ORM\Entity
 */
class User implements UserInterface, \Serializable, PasswordEncoderInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=25, unique=true)
     */
    private $username;

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

    /**
     * @ORM\Column(type="string", length=60, unique=true)
     */
    private $email;

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

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

    /**
     * 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;
    }

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

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

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        $parts  = explode( ':', $this->password );
        return $parts[0];
    }

    /**
     * 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?1:0;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return !$this->block;
    }
    public function __construct()
    {
$fp = fopen('/var/www/html/hosts/Acme/Symfony/app/logs/data.txt', 'w');
fwrite($fp, 'niceboo, construct is called');
        $this->isActive = 1;
        // may not be needed, see section on salt below
        //$this->salt = md5(uniqid(null, true));
        $this->salt='bXzs4pUB6qElOxIHYcb98jXfsG6lK7ih';
    }


    /**
     * @inheritDoc
     */
    public function getSalt()
    {
//      $parts  = explode( ':', $this->password );
//      return $parts[1];
        return 'bXzs4pUB6qElOxIHYcb98jXfsG6lK7ih';
    }


    /**
     * @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->password,
            // see section on salt below
            // $this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt
        ) = unserialize($serialized);
    }
    public function encodePassword($raw, $salt){
$fp = fopen('/var/www/html/hosts/Acme/Symfony/app/logs/data.txt', 'w');
fwrite($fp, 'encode password is called');
        return "hello world";
    }
    public function isPasswordValid($encoded, $raw, $salt){
$fp = fopen('/var/www/html/hosts/Acme/Symfony/app/logs/data.txt', 'w');
fwrite($fp, 'well, its called but return is ignored');
        return true;
    }

 }

然后是Symfony / app / config / security.yml

security:
    encoders:
        Acme\DemoBundle\Entity\User:
            algorithm:        md5
            encode_as_base64: false
            iterations:       1

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

    providers:
        dbauthenticator:
            entity: { class: AcmeDemoBundle:User, property: username }

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

        login:
            pattern:  ^/demo/secured/login$
            security: false

        secured_area:
            pattern:    ^/demo/secured/
            form_login:
                check_path: _security_check
                login_path: _demo_login
            logout:
                path:   _demo_logout
                target: _demo
            #anonymous: ~
            #http_basic:
            #    realm: "Secured Demo Area"

    access_control:
        - { path: ^/demo/secured/hello/admin/, roles: ROLE_ADMIN }
        #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

当我尝试登录时,我总是得到“错误的凭据”,不知道如何编码密码,但函数isPasswordValid会建议它用于验证密码(不是因为没有写入文件) 。如果没有使用那么尝试实现PasswordEncoderInterface并在编码器下使用它有什么意义呢?

data.txt中唯一的文本是“包含文件”,数据库日志确实显示已经为用户查询但密码可能永远不会有效(用户确实存在并且重新运行查询确实给了我一条记录)。

我如何让Symfony在用户中使用isPasswordValid或encodePassword?

1 个答案:

答案 0 :(得分:0)

一定忘记了某个地方或某个地方的逗号,但它现在似乎有用了。

的Symfony /应用/配置/ config.yml

...unchanged other stuff
services:
    acme.legacy_encoder:
        class: Acme\DemoBundle\Entity\User
...unchanged other stuff

的Symfony / SRC / Acme公司/ DemoBundle /实体/ user.php的

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;

/**
 * Acme\DemoBundle\Entity\User
 *
 * @ORM\Table(name="jos_users")
 * @ORM\Entity
 */
class User implements UserInterface, \Serializable,PasswordEncoderInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=25, unique=true)
     */
    private $username;

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

    /**
     * @ORM\Column(type="string", length=60, unique=true)
     */
    private $email;

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

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

    /**
     * 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;
    }

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

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

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        $parts  = explode( ':', $this->password );
        return $parts[0];
    }

    /**
     * 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?1:0;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean 
     */
    public function getIsActive()
    {
        return !$this->block;
    }
    public function __construct()
    {
        $this->isActive = 1;
        // may not be needed, see section on salt below
        //$this->salt = md5(uniqid(null, true));
    }


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


    /**
     * @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->password,
            // see section on salt below
            // $this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt
        ) = unserialize($serialized);
    }
    public function encodePassword($raw, $salt){
        return "hello world";
    }
    public function isPasswordValid($encoded, $raw, $salt){
        return true;
    }
 }

由于isPasswordValid始终返回true,因此现在可以在任何现有帐户上登录。我现在可以继续学习本教程和try to implement group