我正在处理简单的应用程序,它处理两种用户,用户和管理员实体的身份验证。我希望有两个单独的防火墙和提供程序,所以我的security.yml文件看起来像这样:
security:
firewalls:
admin_firewall:
pattern: ^/admin
anonymous: ~
form_login:
check_path: admin_login_check
login_path: admin_login
logout:
path: admin_logout
provider: admin_provider
main_firewall:
pattern: ^/
anonymous: ~
form_login:
check_path: login_check
login_path: login
logout:
path: logout
provider: main_provider
access_control:
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
providers:
main_provider:
entity:
class: My\UserBundle\Entity\User
property: taxId
admin_provider:
entity:
class: My\UserBundle\Entity\Admin
encoders:
My\UserBundle\Entity\User: sha512
My\UserBundle\Entity\Admin: sha512
我的路线:
login:
path: /logowanie
defaults:
_controller: MyUserBundle:Security:login
logout:
path: /wylogowanie
login_check:
path: /login_check
admin_login_check:
path: /admin/login_check
问题很奇怪。使用此配置时,当我使用/ admin / login url打开浏览器时,它会向我显示登录表单,当我使用适当的凭据提交表单时,它会给出以下异常:
exception 'Symfony\Component\Security\Core\Exception\AuthenticationServiceException' with message 'The Doctrine repository "My\UserBundle\Entity\AdminRepository" must implement UserProviderInterface.' in /home/karol/www/my/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94
但我的Admin实体看起来像这样:
<?php
namespace My\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Admin
*
* @ORM\Table(name="my_admin")
* @ORM\Entity(repositoryClass="My\UserBundle\Entity\AdminRepository")
* @UniqueEntity("username")
*/
class Admin implements UserInterface, \Serializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @Assert\NotBlank(message="Proszę podać nazwę użytkownika")
* @ORM\Column(type="string", name="username", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=32)
*/
private $salt;
/**
* @Assert\NotBlank(message="Proszę podać hasło")
* @ORM\Column(type="string", length=255)
*/
private $password;
/**
* @Assert\Email(message="Proszę podać adres e-mail")
* @Assert\NotBlank(message="Proszę podać adres e-mail")
* @ORM\Column(type="string", length=60)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct() {
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
}
/**
* @inheritDoc
*/
public function getUsername() {
return $this->username;
}
/**
* @inheritDoc
*/
public function setUsername($username) {
$this->username = $username;
}
/**
* @inheritDoc
*/
public function getSalt() {
return $this->salt;
}
/**
* @inheritDoc
*/
public function getPassword() {
return $this->password;
}
public function setPassword($password) {
$this->password = $password;
return $this;
}
/**
* @inheritDoc
*/
public function getRoles() {
return array('ROLE_USER');
}
/**
* @inheritDoc
*/
public function eraseCredentials() {
}
/**
* @see \Serializable::serialize()
*/
public function serialize() {
return serialize(array($this->id,));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized) {
list($this->id, ) = unserialize($serialized);
}
public function getTaxId() {
return $this->taxId;
}
public function setTaxId($taxId) {
$this->taxId = $taxId;
return $this;
}
public function getEmail() {
return $this->email;
}
public function setEmail($email) {
$this->email = $email;
return $this;
}
}
答案 0 :(得分:1)
您的实体存储库My \ UserBundle \ Entity \ Admin需要实现UserProviderInterface,或者您需要在提供程序“admin_provider”配置中指定用户名字段
security:
providers:
main:
entity:
class: Acme\UserBundle\Entity\User
property: username
答案 1 :(得分:-1)
您的用户实体实现UserInterface而不是UserProviderInterface
<强> EDITED 强>
例外是:
exception 'Symfony\Component\Security\Core\Exception\AuthenticationServiceException'
带有消息
'The Doctrine repository "My\UserBundle\Entity\AdminRepository" must implement UserProviderInterface.' in /home/karol/www/my/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94
所以要修复它,你应该在你的My \ UserBundle \ Entity \ Admin中更改
class Admin implements UserInterface (...)
为:
class Admin implements UserProviderInterface (...)