0

我正在开发简单的应用程序来处理两种用户的身份验证,用户和管理员实体。我想为此拥有两个单独的防火墙和提供程序,因此我的 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;
}

}
4

2 回答 2

1

您的实体 My\UserBundle\Entity\Admin 存储库需要实现 UserProviderInterface 或者您需要在提供者“admin_provider”配置中指定用户名字段

security:
    providers:
        main:
            entity:
               class: Acme\UserBundle\Entity\User
               property: username
于 2014-02-06T09:21:16.813 回答
-1

您的 User 实体实现了 UserInterface 而不是 UserProviderInterface

已编辑

例外是:

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 (...)
于 2013-11-24T21:39:46.643 回答