与第二个经理进行身份验证

时间:2017-03-10 10:53:26

标签: authentication doctrine symfony

我为第二个数据库创建了第二个管理器。 在这个基础上,我创建了一个包含我的用户的表。 问题是symfony没有从这个数据库加载用户。

这是我的config.yml的摘录:

学说配置

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        seconddb:
            driver:   pdo_mysql
            host:     "xx.xx.xx.xx"
            port:     "3306"
            dbname:   "acme_test"
            user:     "acmegamestest"
            password: "mypassword"
            charset:  UTF8

    # if using pdo_sqlite as your database driver:
    #   1. add the path in parameters.yml
    #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
    #   2. Uncomment database_path in parameters.yml.dist
    #   3. Uncomment next line:
    #     path:     "%database_path%"

orm:
    default_entity_manager: default
    auto_generate_proxy_classes: "%kernel.debug%"
    #naming_strategy: doctrine.orm.naming_strategy.underscore
    entity_managers:
        default:      
           connection: default 
           mappings:
                acmeAdminBundle: ~
                acmeBlogBundle: ~
                gedmo_translatable:
                   type: annotation
                   alias: GedmoTranslatable
                   prefix: Gedmo\Translatable\Entity
                   is_bundle: false
                   # make sure vendor library location is correct
                   dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
        seconddb:
           connection: seconddb
           mappings:
               acmeJoueurBundle: ~ 

这是我的security.yml的摘录:

encoders:
    FOS\UserBundle\Model\UserInterface: sha512
    Acme\JoueurBundle\Entity\Players:
        algorithm: sha512
        encode_as_base64: false

providers:
    seconddb:
        entity:
            class: Acme\JoueurBundle\Entity\Players
            property: username    
            manager_name: seconddb
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    administration:
        pattern: ^/admin
        provider: fos_userbundle
        context: administration
        form_login:
            #csrf_provider: security.csrf.token_manager
            login_path : fos_user_security_login
            check_path : fos_user_security_check
            failure_path : null
            default_target_path : /admin                
        logout:       
          path : fos_user_security_logout
          target : /connexion
        anonymous: true    
    frontend:
        pattern: ^/
        provider: acme_joueurbundle
        context: frontend
        form_login:
            #csrf_provider: form.csrf_provider
            login_path : acme_players_login
            check_path : acme_players_check
            failure_path : null
            default_target_path : acme_players_userprofile                      
        logout:       
          path : acme_players_logout
          target : acme_players_login
        anonymous:    true

我的实体实现" AdvancedUserInterface,\ Serializable" 有了这个功能:

 //////////////////////////liaison pour symfony////////////////////////////

public function getRoles()
{
     return array('ROLE_PLAYERS');
}

public function getSalt(){

    return $this->salt;
}

public function eraseCredentials(){

}

public function isAccountNonExpired()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{

    return !$this->banned;
}

public function isEnabled()
{
    return $this->active;
}



    /** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password,
        $this->active
        // see section on salt below
        // $this->salt,
    ));
}

/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password,
        $this->active
        // see section on salt below
        // $this->salt
    ) = unserialize($serialized);
}

/////////////////////////////////////////////////////////////////////////////

但是当我尝试连接时,我在转储功能中遇到了这个错误:

enter image description here

在Doctrine部分我得到了这个:

enter image description here

您知道解决方案吗? 希望您能够帮助我。 感谢

1 个答案:

答案 0 :(得分:0)

修改

因此,错误来自用户身份验证提供程序中的这段代码:

try {
    $user = $this->retrieveUser($username, $token);
} catch (UsernameNotFoundException $e) {
    if ($this->hideUserNotFoundExceptions) {
        throw new BadCredentialsException('Bad credentials.', 0, $e);
    }
    $e->setUsername($username);
    throw $e;
}

方法retrieveUser失败,它会抛出错误的凭据异常。这解释了为什么没有连接,因为它还没有被调用。

在向您询问提供商的代码之前,让我们看一下您的security.yml,因为您创建了一个名为seconddb的提供程序,并且在您的防火墙前端尝试调用提供者acme_joueurbundle,他可能没有存在。

所以,你的security.yml应该是这样的:

# app/config/security.yml

[...]

providers:
    seconddb:
        entity:
            class: Acme\JoueurBundle\Entity\Players
            property: username    
            manager_name: seconddb
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    administration:
        provider: fos_userbundle
        [...]
    frontend:
        provider: seconddb
        [...]