简体   繁体   English

symfony2 FOSUserBundle登录返回'bad credentials'

[英]symfony2 FOSUserBundle login returns 'bad credentials'

I'm new to FOSUserBundle and it's some hours that i'm struggling with this error...and can't find appropriate answer in the site. 我是FOSUserBundle的新手,我正在努力解决这个错误......并且在网站上找不到合适的答案。

can anyone help me plz?:-* 任何人都可以帮助我PLZ?: - *

this is my child user entity 这是我的孩子用户实体

<?php
// src/Blogger/BlogBundle/Entity/CommonUser.php

namespace Blogger\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;


/**
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\Repository\CommonUserRepository")
 * @ORM\Table(name="CommonUser")
 * @ORM\HasLifecycleCallbacks
 */
class CommonUser extends BaseUser
{

    /**
     * @ORM\OneToMany(targetEntity="Request", mappedBy="commonUser")
     */
    protected $requests;



    public function __construct()
    {
        parent::__construct();

        $this->requests = new ArrayCollection();


    }



    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


...
...
...

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


    public function getSalt()
    {
        return '';
    }
...

}

and also this is the request.php class 这也是request.php类

<?php
// src/Blogger/BlogBundle/Entity/Request.php

namespace Blogger\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Blogger\BlogBundle\Entity\Repository\RequestRepository;
/**
 * @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\Repository\requestRepository")
 * @ORM\Table(name="request")
 * @ORM\HasLifecycleCallbacks
 */
class Request
{

    /**
     * @ORM\OneToMany(targetEntity="Note", mappedBy="request")
     */
    protected $notes;

    public function __construct()
    {
        $this->notes = new ArrayCollection();


        $this->setCreated(new \DateTime());
        $this->setUpdated(new \DateTime());
    }

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="CommonUser", inversedBy="requests")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $commonUser;

    /**
     * @ORM\Column(type="text")
     */
    protected $request;

    ....
    ....
    ....



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

    ....
    ....
    ....
}

and this is my security file 这是我的安全文件

# app/config/security.yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }

When you register with the provided controller from FOSUserBundle the password stored in DB is encrypted with 500 times SHA1 and the random generated salt. 当您从FOSUserBundle向提供的控制器注册时,存储在DB中的密码将使用500倍SHA1和随机生成的盐进行加密。 So if you always return "" by the function getSalt() the password will never match. 因此,如果您总是通过函数getSalt()返回“”,则密码永远不会匹配。 do not override this function. 不要覆盖此功能。

you can however overwrite the Controllers in FOSUserBundle, more about that in the offical documentation . 但是,你可以在FOSUserBundle中覆盖控制器,更多关于官方文档中的控制器。

public function getSalt()
{
    return '';
}

Just try to update the password. 只是尝试更新密码。 In the terminal enter: 在终端输入:

php fos:user:change-password $username $password

where $user is the user name and $password the new password. 其中$user是用户名, $password是新密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM