【问题标题】:multiple firewalls and providers in symfony2symfony2 中的多个防火墙和提供程序
【发布时间】:2013-06-20 21:56:48
【问题描述】:

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

}

【问题讨论】:

    标签: authentication symfony symfony-2.3


    【解决方案1】:

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

    security:
        providers:
            main:
                entity:
                   class: Acme\UserBundle\Entity\User
                   property: username
    

    【讨论】:

      【解决方案2】:

      您的用户实体实现了 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 (...)
      

      【讨论】:

      • 请解释更多:这看起来是理解的缩写。
      • 我认为问题只是在admin_provider 中缺少property。并且没有理由将UserInterface 替换为UserProviderInterface,类Admin 仍应实现UserInterface,因为它是用户。即使只是将UserProviderInterface 添加到Admin 类,这也是一种责任混合。
      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多