Php symfony 2.4始终获取错误凭据错误

Php symfony 2.4始终获取错误凭据错误,php,symfony,login,Php,Symfony,Login,我正在使用Symfony 2.4框架编写一个网站,但由于某些原因,登录过程不起作用 我总是收到这样的信息:糟糕的凭证 app/logs/dev.log输出 [2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] [] [2013-1

我正在使用Symfony 2.4框架编写一个网站,但由于某些原因,登录过程不起作用

我总是收到这样的信息:糟糕的凭证

app/logs/dev.log输出

[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". [] []
[2013-12-15 02:16:23] request.INFO: Matched route "security_login_check" (parameters: "_route": "security_login_check") [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2013-12-15 02:16:23] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2013-12-15 02:16:23] doctrine.DEBUG: SELECT t0.id AS id1, t0.user_name AS user_name2, t0.group_id AS group_id3, t0.pwd AS pwd4, t0.salt AS salt5, t0.email AS email6, t0.description AS description7, t0.is_pay AS is_pay8, t0.reg_at AS reg_at9, t0.last_login_at AS last_login_at10, t0.pay_at AS pay_at11, t0.pay_expire_at AS pay_expire_at12, t0.delete_flag AS delete_flag13, t0.raw_add_time AS raw_add_time14, t0.raw_update_time AS raw_update_time15, t0.group_id AS group_id16 FROM User t0 WHERE t0.user_name = ? LIMIT 1 ["test002"] []
[2013-12-15 02:16:23] security.INFO: Authentication request failed: Bad credentials [] []
[2013-12-15 02:16:23] security.DEBUG: Redirecting to /login [] []
使用上面的sql,我可以在数据库中找到数据

这是我的保安

security:
encoders:
    Bestxtech\UserBundle\Entity\User: sha512

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

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Entity\User, property: userName }

acl:
    connection: default

firewalls:
    main:
        pattern:    ^/
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /
        anonymous: true

        remember_me:
            key:      "%secret%"
            lifetime: 31536000
            path:     /
            domain:   ~

access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
My User.php类

<?php
namespace Bestxtech\UserBundle\Model;

/**
 * User Model
 * 
 * @author Terry Gao <gavntery@gmail.com>
 */
abstract class User implements UserInterface
{
    /**
     * @var integer
     */
    protected $id;

    /**
     * @var string
     */
    protected $userName;

    /**
     * @var integer
     */
    protected $groupId;

    /**
     * @var string
     */
    protected $pwd;

    /**
     * @var string
     */
    protected $salt;

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

    /**
     * @var string
     */
    protected $description;

    /**
     * @var boolean
     */
    protected $isPay;

    /**
     * @var \DateTime
     */
    protected $regAt;

    /**
     * @var \DateTime
     */
    protected $lastLoginAt;

    /**
     * @var \DateTime
     */
    protected $payAt;

    /**
     * @var \DateTime
     */
    protected $payExpireAt;

    /**
     * @var boolean
     */
    protected $deleteFlag;

    /**
     * @var \DateTime
     */
    protected $rawAddTime;

    /**
     * @var \DateTime
     */
    protected $rawUpdateTime;

    /**
     * @var \Bestxtech\UserBundle\Entity\Usergroup
     */
    protected $group;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->regAt = new \DateTime("now");
        $this->salt    = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
        $this->deleteFlag = 0;
        $this->rawAddTime = new \DateTime("now");
    }

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

    /**
     * {@inheritDoc}
     */
    public function setUserName($userName)
    {
        $this->userName = $userName;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setGroupId($groupId)
    {
        $this->groupId = $groupId;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setPwd($pwd)
    {
        $this->pwd = $pwd;

        return $this;
    }

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

    public function getPassword() 
    {
        return $this->getPwd();
    }

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

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

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setIsPay($isPay)
    {
        $this->isPay = $isPay;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setRegAt($regAt)
    {
        $this->regAt = $regAt;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setLastLoginAt($lastLoginAt)
    {
        $this->lastLoginAt = $lastLoginAt;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setPayAt($payAt)
    {
        $this->payAt = $payAt;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setPayExpireAt($payExpireAt)
    {
        $this->payExpireAt = $payExpireAt;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setDeleteFlag($deleteFlag)
    {
        $this->deleteFlag = $deleteFlag;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setRawAddTime($rawAddTime)
    {
        $this->rawAddTime = $rawAddTime;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setRawUpdateTime($rawUpdateTime)
    {
        $this->rawUpdateTime = $rawUpdateTime;

        return $this;
    }

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

    /**
     * {@inheritDoc}
     */
    public function setGroup(\Bestxtech\UserBundle\Entity\Usergroup $group = null)
    {
        $this->group = $group;

        return $this;
    }

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

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

    /**
     * {@inheritDoc}
     */
    public function eraseCredentials()
    {
        $this->pwd = null;
    }
}

您必须从FOSUserBundle的
BaseUser
实体扩展
User
实体,以
username
username\u canonical
的形式访问用户的基本属性

/**
*用户模型
* 
*@ORM\Entity
*@ORM\Table(name=“User”)
*@作者高泰瑞
*/
抽象类用户实现UserInterface扩展BaseUser
{
//你自己的逻辑。。。
}

您的用户类的名称空间中似乎有错误

encoders:
    Bestxtech\UserBundle\Model\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Model\User, property: userName }
而不是

encoders:
    Bestxtech\UserBundle\Entity\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Entity\User, property: userName }

最后,我找到了原因。代码很好,但是dadabase“User”表中pwd列的长度是45,这太大了。更改为100,然后就可以了。

添加隐藏用户未找到:在security.yml文件的security下直接为false。然后,错误消息将显示是否找不到用户或密码问题。这是密码问题,SQL将返回用户,如OP所述。如何创建用户?您是否正确地散列了密码?@Cerad错误消息说“提供的密码无效”。@Julien Yes。在security.yml中,它使用sha512作为编码器,我确信它使用编码器和salt来保存用户pwd。但如何确保登录时使用编码器和salt?谢谢大家!至少你知道找到用户是成功的一半。我会直接深入到Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder中,并在isPasswordValid中放入一些调试语句。UserInterface在与User类相同的命名空间中定义。用户界面从Symfony\Component\Security\Core\User\UserInterface扩展为BaseUserInterface。
/**
 * User Model
 * 
 * @ORM\Entity
 * @ORM\Table(name="User")
 * @author Terry Gao <gavntery@gmail.com>
 */
abstract class User implements UserInterface extends BaseUser
{
    // Your own logic ...
}
encoders:
    Bestxtech\UserBundle\Model\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Model\User, property: userName }
encoders:
    Bestxtech\UserBundle\Entity\User: sha512

providers:
    bestxtech_user:
        entity: { class: Bestxtech\UserBundle\Entity\User, property: userName }