[英]Symfony: Load Security Users from the Database
I read the official documentation of Symfony in order to give access to a specific page. 我阅读了Symfony的官方文档,以便访问特定页面。 I followed the instructions, but when I insert user and pw the I am not redirected to protected page. 我遵循了说明,但是当我插入用户和密码时,我没有重定向到受保护的页面。
This is the entity 这是实体
namespace AppBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_ADMIN');
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
This is the security.yml 这是security.yml
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_ADMIN
ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
providers:
our_db_provider:
entity:
class: AppBundle:User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
main:
anonymous: ~
http_basic: ~
default:
pattern: ^/
http_basic: ~
provider: our_db_provider
access_control:
- { path: ^/dashboard/admin, roles: ROLE_ADMIN }
The password has been inserted manually in the database using this BCrypt tool 使用此BCrypt 工具已将密码手动插入数据库中
What I am doing wrong? 我做错了什么?
I am not a specialist, but I think you forget to specify some stuff in your security file in the firewalls definition. 我不是专家,但是我认为您忘记在防火墙定义中的安全文件中指定一些内容。 You can find mine below. 您可以在下面找到我的。
form_login:
login_path: fos_user_security_login
check_path: fos_user_security_check
default_target_path: route to your logged user should go after logged
logout:
path: fos_user_security_logout
target: route to your user should go after logout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.