doulan3966 2016-03-28 09:28
浏览 46

Symfony / doctrine:无法登录数据库用户,在security.yml中不正确?

For the past few days I've been struggling with logging in a database-user using Symfony/Doctrine, and I'm pretty stuck by now (I'm new to Symfony, btw). I got the login working using the in_memory provider (to start with), but now I want to login using a database-user and I just don't know what's wrong.

I went over the documentation over and over and I think I got everything right. I don't get errors, it just says "Invalid credentials." when I try to login. I have to feeling that it has something to do with security.yml but I've tried about all I could find. Below is my code;

security.yml;

security:
providers:
    provider_users:
        entity:
            class: AppBundle:User
            property: username

encoders:
    AppBundle\Entity\User:
        algorithm: bcrypt

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

    main:
        pattern: ^/
        anonymous: ~
        provider: provider_users
        form_login:
            login_path: login
            check_path: login

access_control:
    - { path: ^/admin, roles: ROLE_ADMIN }

User.php;

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

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

/**
 * AppBundle\Entity\User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
 */
class User implements UserInterface, \Serializable
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

    /**
     * @ORM\Column(name="roles", type="string")
     */
    private $roles;

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

    public function __construct()
    {
        $this->isActive = true;
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
    }

    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);
    }

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

        return $this;
    }

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

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

        return $this;
    }

    /**
     * Get roles
     *
     * @return string[]
     */
    public function getRoles()
    {
        return json_decode($this->roles);
    }

    /**
     * Set isActive
     *
     * @param boolean $isActive
     *
     * @return User
     */
    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;

        return $this;
    }

    /**
     * Get isActive
     *
     * @return boolean
     */
    public function getIsActive()
    {
        return $this->isActive;
    }
}

I also tested if I could just get the user "manually" using getRepository()->findAll(), and that looks okay;

array (size=1)
0 => 
    object(AppBundle\Entity\User)[323]
      private 'id' => int 20
      private 'username' => string 'user' (length=4)
      private 'salt' => string 'mqshzqa9syok0kw8ss4cscc84k4k804' (length=31)
      private 'password' => string 'user1' (length=5)
      private 'email' => string 'user@localhost.com' (length=18)
      private 'roles' => string '' (length=0)
      private 'isActive' => boolean true

success!

Any suggestions? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doudu3961 2016-03-28 10:06
    关注

    you need to encrypt the password with bcrypt. Possibly the user was in the database before adding security. Try to add another user with the encrypted password in php have for example: string crypt (string $ str [, string $ salt ] )

    The string that you insert in the password field User : user-> setPassword ('your encrypted string')

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?