我正在学习如何在 Symfony2 中使用身份验证,用户名来自实体(MySQL 数据库)。我在 PhpMyAdmin 创建了一个简单的表:
INSERT INTO `user` (`id`, `username`, `password`, `salt`, `roles`) VALUES
(1, 'bobby4078', 'test', '', ''),
(2, 'damien', 'test', '', ''),
(3, 'thomas', 'test', '', '');
如您所见,只有 3 个用户,使用相同的密码“test”,没有盐和角色。
然后我编辑了我的 security.yml :
jms_security_extra:
secure_all_services: false
expressions: true
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
Rtf\UserBundle\Entity\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
main:
entity: { class: RtfUserBundle:User, property: username }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
provider: main
form_login:
login_path: login
check_path: login_check
logout:
path: logout
target: /
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
#- { path: ^/_internal/secure, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
最后,这是我的实体(我希望这不会太长):
namespace Rtf\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Rtf\UserBundle\Entity\UserRepository")
*/
class User implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="salt", type="string", length=255)
*/
private $salt;
/**
* @var array
*
* @ORM\Column(name="roles", type="array")
*/
private $roles;
public function _construct(){
$this->roles = array();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* 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 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 roles
*
* @param array $roles
* @return User
*/
public function setRoles($roles)
{
$this->roles = $roles;
return $this;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function eraseCredentials(){
}
}
问题是当我在表单中输入登录名和密码时,在我的 symfony 工具栏中我可以看到用户仍然是“匿名”。所以它没有在我的数据库中正确读取......
我真的不知道这可能来自哪里,我不知道从哪里开始搜索我的代码中的错误......
有什么帮助吗?
谢谢,对不起我的英语不好。