Php 密码加密/解密在Symfony 3.0中失败

Php 密码加密/解密在Symfony 3.0中失败,php,encryption,symfony,Php,Encryption,Symfony,按照文献记载的通过条令和传统形式设置用户身份验证的方法,我最终陷入了一个奇怪的境地:用户创建以与解码不同的方式对密码进行编码,因此实际上我的用户无法登录(无效凭据错误)。但是,使用一些旧的散列(来自早期项目)并直接更新数据库,所有密码都可以设置为“admin”,并且工作正常。但这种方法显然不能维持很长时间 security.yml security: providers: db_provider: entity: c

按照文献记载的通过条令和传统形式设置用户身份验证的方法,我最终陷入了一个奇怪的境地:用户创建以与解码不同的方式对密码进行编码,因此实际上我的用户无法登录(无效凭据错误)。但是,使用一些旧的散列(来自早期项目)并直接更新数据库,所有密码都可以设置为“admin”,并且工作正常。但这种方法显然不能维持很长时间

security.yml

security:
    providers:
        db_provider:
            entity:
                class: AppBundle:Felhasznalo
                property: username         
    encoders:
        AppBundle\Entity\Felhasznalo:
            algorithm: bcrypt
    firewalls:
        default:
            pattern: ^/
            provider: db_provider
            anonymous: true            
            form_login:
                login_path: login
                check_path: login
                default_target_path: admin_home
            logout:
                path: logout
                target: /login    
FelhasznaloController(扩展用户控制器)

安全控制器

public function newAction(Request $request) {
    $entity = new Felhasznalo();
    $form = $this->createForm('AppBundle\Form\FelhasznaloType', $entity);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $encoder = $this->container->get('security.password_encoder');
        $encoded = $encoder->encodePassword($entity, $entity->getPassword());
        $entity->setPassword($encoded);
        $em->flush();
        return $this->redirect($this->generateUrl('admin_felhasznalo_show', array('id' => $entity->getId())));
    }

    return array(
            'felhasznalo' => $entity,
            'form' => $form->createView(),
    );
}
/**
 * @Route("/login", name="login")
 */
public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');
    $error = $authenticationUtils->getLastAuthenticationError();
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'admin/security/login.html.twig',
        array(
                'last_username' => $lastUsername,
                'error'         => $error,
        )
    );
}
Felhasznalo.php(用户实体)


有人能发现en/解码失败的原因吗?目前,我只能由迁移的用户登录,而这些用户的密码在数据库中直接设置为相同的密码。无论如何,通过表单创建用户效果很好,除了生成的哈希之外,它是不可用的。

那么问题只存在于新用户?您不是在用户表中存储盐吗?似乎不太可能基于您发布的代码,但可能希望进行检查。因此,问题仅限于新用户?您不是在用户表中存储盐吗?似乎不太可能基于您发布的代码,但可能希望进行检查。
namespace AppBundle\Entity;

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

/**
 * Felhasznalo
 *
 * @ORM\Table(name="felhasznalo")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\FelhasznaloRepository")
 */
class Felhasznalo implements UserInterface, \Serializable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

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

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

    public function getSalt() {
        // not needed for bcript encoding
        return null;
    }

    public function eraseCredentials() {

    }

    /** @see \Serializable::serialize() */
    public function serialize() {
        return serialize(array(
                $this->id,
                $this->username,
                $this->password,
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized) {
        list (
                $this->id,
                $this->username,
                $this->password,
                ) = unserialize($serialized);
    }
}