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

Posted

技术标签:

【中文标题】Symfony3 +“由于系统原因无法处理身份验证请求”【英文标题】:Symfony3 + "Authentication request could not be processed due to a system" 【发布时间】:2017-11-27 06:34:13 【问题描述】:

我使用 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;
    




你知道为什么吗?

感谢您的帮助!

【问题讨论】:

日志文件对错误的描述是什么? 身份验证请求失败。 "exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationServiceException(code: 0): 身份验证失败。 在 /data/bas/site/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94,MongoConnectionException(代码:11):身份验证失败的。在 /data/bas/site/vendor/alcaeus/mongo-php-adapter/lib/Alcaeus/MongoDbAdapter/ExceptionConverter.php:83,MongoDB\\Driver\\Exception\\AuthenticationException(代码:11):身份验证失败。在 /data/bas/site/vendor/mongodb/mongodb/src/Operation/Find.php:219)" 终于找到解决办法了!当您的连接有用户名/密码时,您必须设置“authSource”参数! @watb 请在哪里设置“authSource”? 【参考方案1】:

此错误消息过于笼统,您需要查看 /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

【讨论】:

以上是关于Symfony3 +“由于系统原因无法处理身份验证请求”的主要内容,如果未能解决你的问题,请参考以下文章