简体   繁体   English

Symfony2-无法登录(用户存储库)

[英]Symfony2 - can't log in (user repository)

I am in a bit of a problem... Folowed the handbook of Symfony2 and now i am stuck with logging myself in :s 我有点问题...看了Symfony2的手册,现在我被困在:s中

Any help would be welcome for this newbie. 任何帮助将欢迎这个新手。 so this is my users entity: 所以这是我的用户实体:

<?php

namespace SocialGeo\BackendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface; 
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* SocialGeo\BackendBundle\Entity\Users
*/
class Users implements AdvancedUserInterface
{
/**
 * @var integer $userId
 */
private $userId;

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

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

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

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

/**
 * @var boolean $userActive
 */
private $userActive;

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

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

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
private $rolesRole;


/**
 * Constructor
 */
public function __construct()
{
    $this->rolesRole = new ArrayCollection();
    $this->salt = md5(uniqid(null, true));
}

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

/**
 * Set username
 *
 * @param string $username
 * @return Users
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

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

/**
 * Set userPassword
 *
 * @param string $userPassword
 * @return Users
 */
public function setUserPassword($userPassword)
{
    $this->userPassword = $userPassword;

    return $this;
}

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

/**
 * Set userEmail
 *
 * @param string $userEmail
 * @return Users
 */
public function setUserEmail($userEmail)
{
    $this->userEmail = $userEmail;

    return $this;
}

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

/**
 * Set userActive
 *
 * @param boolean $userActive
 * @return Users
 */
public function setUserActive($userActive)
{
    $this->userActive = $userActive;

    return $this;
}

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

/**
 * Set userFavourites
 *
 * @param string $userFavourites
 * @return Users
 */
public function setUserFavourites($userFavourites)
{
    $this->userFavourites = $userFavourites;

    return $this;
}

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

/**
 * Set userScore
 *
 * @param integer $userScore
 * @return Users
 */
public function setUserScore($userScore)
{
    $this->userScore = $userScore;

    return $this;
}

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

/**
 * Add rolesRole
 *
 * @param SocialGeo\BackendBundle\Entity\Roles $rolesRole
 * @return Users
 */
public function addRolesRole(\SocialGeo\BackendBundle\Entity\Roles $rolesRole)
{
    $this->rolesRole[] = $rolesRole;

    return $this;
}

/**
 * Remove rolesRole
 *
 * @param SocialGeo\BackendBundle\Entity\Roles $rolesRole
 */
public function removeRolesRole(\SocialGeo\BackendBundle\Entity\Roles $rolesRole)
{
    $this->rolesRole->removeElement($rolesRole);
}

/**
 * Get rolesRole
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getRolesRole()
{
    return $this->rolesRole->toArray();
}

public function eraseCredentials() 
{

}

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

public function getRoles() 
{
    //return $this->groups->toArray();
    return $this->getRolesRole();
}

public function getSalt() 
{
    return $this->salt;
}
public function isEqualTo(UserInterface $users)
{
    return $this->username === $users->getUsername();
}

public function isAccountNonExpired() {
    return true;
}

public function isAccountNonLocked() {
    return true;
}

public function isCredentialsNonExpired() {
    return true;
}

public function isEnabled() {
    return $this->userActive;
}
}

my roles entity: 我的角色实体:

<?php

namespace SocialGeo\BackendBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * SocialGeo\BackendBundle\Entity\Roles
 */
class Roles implements RoleInterface
{
/**
 * @var integer $roleId
 */
private $roleId;

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

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

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

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
private $usersUser;

/**
 * Constructor
 */
public function __construct()
{
    $this->usersUser = new ArrayCollection();
}

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

/**
 * Set roleName
 *
 * @param string $roleName
 * @return Roles
 */
public function setRoleName($roleName)
{
    $this->roleName = $roleName;

    return $this;
}

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

/**
 * Set roleDescription
 *
 * @param string $roleDescription
 * @return Roles
 */
public function setRoleDescription($roleDescription)
{
    $this->roleDescription = $roleDescription;

    return $this;
}

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

/**
 * Add usersUser
 *
 * @param SocialGeo\BackendBundle\Entity\Users $usersUser
 * @return Roles
 */
public function addUsersUser(\SocialGeo\BackendBundle\Entity\Users $usersUser)
{
    $this->usersUser[] = $usersUser;

    return $this;
}

/**
 * Remove usersUser
 *
 * @param SocialGeo\BackendBundle\Entity\Users $usersUser
 */
public function removeUsersUser(\SocialGeo\BackendBundle\Entity\Users $usersUser)
{
    $this->usersUser->removeElement($usersUser);
}

/**
 * Get usersUser
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getUsersUser()
{
    return $this->usersUser;
}

public function getRole() {
    return $this->role;
}
}

userrepository entity: 用户存储库实体:

<?php

namespace SocialGeo\BackendBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * SocialGeo\BackendBundle\Entity\Roles
 */
class Roles implements RoleInterface
{
/**
 * @var integer $roleId
 */
private $roleId;

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

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

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

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
private $usersUser;

/**
 * Constructor
 */
public function __construct()
{
    $this->usersUser = new ArrayCollection();
}

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

/**
 * Set roleName
 *
 * @param string $roleName
 * @return Roles
 */
public function setRoleName($roleName)
{
    $this->roleName = $roleName;

    return $this;
}

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

/**
 * Set roleDescription
 *
 * @param string $roleDescription
 * @return Roles
 */
public function setRoleDescription($roleDescription)
{
    $this->roleDescription = $roleDescription;

    return $this;
}

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

/**
 * Add usersUser
 *
 * @param SocialGeo\BackendBundle\Entity\Users $usersUser
 * @return Roles
 */
public function addUsersUser(\SocialGeo\BackendBundle\Entity\Users $usersUser)
{
    $this->usersUser[] = $usersUser;

    return $this;
}

/**
 * Remove usersUser
 *
 * @param SocialGeo\BackendBundle\Entity\Users $usersUser
 */
public function removeUsersUser(\SocialGeo\BackendBundle\Entity\Users $usersUser)
{
    $this->usersUser->removeElement($usersUser);
}

/**
 * Get usersUser
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getUsersUser()
{
    return $this->usersUser;
}

public function getRole() {
    return $this->role;
}
}

and last one: security.yml: 最后一个:security.yml:

security:
 encoders:
    SocialGeo\BackendBundle\Entity\Users:
        algorithm: sha1
        encode_as_base64: false
        iterations: 1 

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

 providers:
  users:
    entity: { class: SocialGeoBackendBundle:Users }

 firewalls:
    admin_area:
        pattern: ^/users
        http_basic: ~

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

The problem is that my app keeps asking me to log in everytime, but i can't get in (everytimei go to /users page). 问题是我的应用程序每次都要求我登录,但我无法进入(每次都进入/ users页面)。 Home is accesible. 家是可访问的。

So when i go to /users a basic http: pops out of the browser and asks me my credentials, when i fill them in and press enter, i get the same popup of the browser, asking me to log in... 因此,当我进入/ users一个基本的http:时,会从浏览器中弹出并询问我的凭据,当我填写它们并按Enter时,我会得到与浏览器相同的弹出窗口,并要求我登录...

edit: my salt in the database for evey user is: 7308e59b97f6957fb42d66f894793079 and my password for everyuser is 'pass' hashed with sha1 to : 9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684 编辑:我在evey用户的数据库中的盐是:7308e59b97f6957fb42d66f894793079,并且我的每个用户的密码都用sha1散列为“ pass”到:9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684

Your password is hashed incorrectly. 您的密码被错误地散列。 You're supposed to use the salt together with the cleartext password. 您应该将盐与明文密码一起使用。 Try prefixing your password with the salt before hashing it. 在对密码进行哈希处理之前,请尝试先在密码前面加上盐。

update users set password = sha1(concat('7308e59b97f6957fb42d66f894793079', 'pass'))

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

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