php - 什么是用于从数据库获取用户的正确 security.yml 文件

标签 php mysql symfony doctrine-orm

我的 security.yml 文件有一些问题:

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
    # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
    encoders:
        #Symfony\Component\Security\Core\User\User: plaintext
        Login\Loginbundle\Entity\User: sha512

    # http://symfony.com/doc/current/book/security.html#hierarchical-roles
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        users:
            entity: { class: LoginLoginBundle:User, property: username }
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    # the main part of the security, where you can set up firewalls
    # for specific sections of your app
    firewalls:
        secured_area:
            pattern:   ^/

            anonymous: ~
            form_login:
                provider: users
                login_path: login
                check_path: login_check
    access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

我有一个带有登录表单的网站。用户位于数据库中。 我可以使用用户名:user 和密码:userpass 登录,但我怎样才能让它与数据库中的用户一起使用?

我已经阅读了有关 UserInterfaces 的内容并尝试使用它,但没有成功。

也许用户实体有帮助,这里是:

<?php

namespace Login\LoginBundle\Entity;

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

/**
 * User
 */
class User implements UserInterface, \Serializable
{
    /**
     * @var string
     */
    private $username;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $password;

    /**
     * @var integer
     */
    private $money;

    /**
     * @var integer
     */
    private $userid;

    /**
     * @var \Login\LoginBundle\Entity\Team
     */
    private $teamTeamid;

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

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

    /**
     * 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 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 password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

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

    /**
     * Set money
     *
     * @param integer $money
     * @return User
     */
    public function setMoney($money)
    {
        $this->money = $money;

        return $this;
    }

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

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

    /**
     * Set teamTeamid
     *
     * @param \Login\LoginBundle\Entity\Team $teamTeamid
     * @return User
     */
    public function setTeamTeamid(\Login\LoginBundle\Entity\Team $teamTeamid = null)
    {
        $this->teamTeamid = $teamTeamid;

        return $this;
    }

    /**
     * Get teamTeamid
     *
     * @return \Login\LoginBundle\Entity\Team 
     */
    public function getTeamTeamid()
    {
        return $this->teamTeamid;
    }
}

编辑 security.yml 文件和访问数据库用户的正确方法是什么?

最佳答案

您是否尝试使用数据库中的用户登录?

如果您尝试使用名称“user”登录并传递“userpass”,这应该有效。

如果您想使用数据库中的用户,您应该像这样编辑您的 security.yml 文件

providers:
        user:
            entity:
                class: Login\LoginBundle\Entity\User
                property: username

关于php - 什么是用于从数据库获取用户的正确 security.yml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619011/

相关文章:

php - Laravel 无法连接到数据库

php - 从 mysql 表中回显行作为 html 下拉值的结果

mysql - SQL - 查找键等于值的所有 "Relationship IDs",然后选择包含这些 "Relationship IDs"的所有记录

C# 将图像作为 blob 保存到 MySql 数据库

php - Symfony 5.1.3 清除缓存时出现问题 : Attempted to load class "MappingDriverChain" from namespace

php - 高级搜索页面上的实时 MySQL 搜索结果

php - 无法从表中检索数据?

php - Laravel 中的自定义查询

php - Symfony:违反完整性约束:500 内部服务器错误

php - Twig - 在数据描述中的 href 中替换双引号