Symfony3 +“由于系统无法处理身份验证请求”

时间:2017-06-23 15:51:17

标签: mongodb symfony security

我将Symfony3与MongoDB ODM一起使用。 当我尝试登录时,我收到此消息:“由于系统问题,无法处理身份验证请求。”

这是security.yml文件:

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/security.html
security:
    encoders:
        PlannerBundle\Document\Account: sha512

    # http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
    providers:
        account_provider:
            mongodb: { class: PlannerBundle\Document\Account, property: username }
        in_memory:
            memory: ~

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_COMMERCIAL:  ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN]

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        admin:
            pattern: ^/admin
            form_login:
                provider: account_provider
                check_path: login
                login_path: login
                default_target_path: sonata_admin_dashboard

            logout:
                path:   logout
                target: /admin/login

            anonymous:    true
            logout: true

    access_control:
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: [ROLE_ADMIN, ROLE_COMMERCIAL] }

实体文件:

<?php
// src/PlannerBundle/Document/Account.php
namespace PlannerBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document(collection="users", repositoryClass="PlannerBundle\Repository\AccountRepository")
 */
class Account implements UserInterface, \Serializable
{
    /**
     * @MongoDB\Id(strategy="UUID", type="string")
     */
    protected $id;

    /**
     * @MongoDB\Field(type="string")
     * @MongoDB\Index(unique=true)
     */
    private $username;

    /**
     * @MongoDB\Field(type="string")
     */
    private $password;

    /**
     * @MongoDB\Field(type="string")
     * @MongoDB\Index(unique=true)
     */
    private $email;

    /**
     * @MongoDB\Field(type="collection")
     */
    private $roles;

    /**
     * @MongoDB\Field(type="boolean")
     */
    private $isActive;

    public function __construct()
    {
        $this->isActive = true;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getSalt()
    {
        return null;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getRoles()
    {
        return $this->roles;
    }

    public function eraseCredentials()
    {
    }

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

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

    /**
     * Get id
     *
     * @return string $id
     */
    public function getId()
    {
        return $this->id;
    }



}

你知道为什么吗?

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

此错误消息过于笼统,您需要查看/logs/dev.log来查找您的特定问题。搜索错误文本,例如显示为"[Semantical Error] line 0, col 44 near 'username = :username': Error: 'username' is not defined."的错误文本,然后我必须在security.yml中添加下面这个示例的最后一行**"property: username"**

# To get started with security, check out the documentation:
# https://symfony.com/doc/current/security.html
security:
    encoders:
        AppBundle\Entity\User:
            algorithm: bcrypt

# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
    db_provider:
        entity:
            class: AppBundle:User
            property: username