在链配置的名称空间中找不到类“ App \ Entity \ User”

时间:2019-03-19 20:54:45

标签: authentication symfony-4.2

我遇到了错误:

  

在链配置的名称空间中找不到类'App \ Entity \ User'

我正在使用API​​平台运行Symfony 4.2。我需要创建一个API令牌/密钥身份验证设置,并正在使用防护身份验证器。

实体:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table(name="arc_sync_api_keys")
 */
class User implements UserInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    public $id;

    /** @ORM\Column(length=20) */
    public $username;

    /** @ORM\Column(name="api_token", length=40) */
    public $apiKey;

    /** @ORM\Column(length=30) */
    public $roles = [];

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

    public function getRoles(): array
    {
        return array('ROLE_USER');
    }

    public function getPassword()
    {
    }
    public function getSalt()
    {
    }
    public function eraseCredentials()
    {
    }
}

验证者:

namespace App\Security;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;

class ApiKeyAuthenticator extends AbstractGuardAuthenticator
{
    ...

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $apiKey = $credentials['token'];

        if (null === $apiKey) {
            return;
        }



        // if a User object, checkCredentials() is called
        return $userProvider->loadUserByUsername($apiKey);
    }

    ...
}

security.yaml

security:
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: ~
            logout: ~

            guard:
                authenticators:
                    - App\Security\ApiKeyAuthenticator

错误发生在这里:

  

返回$ userProvider-> loadUserByUsername($ apiKey);

在通过它进行操作后无法加载驱动程序,但是我不知道如何解决此问题。谢谢!

1 个答案:

答案 0 :(得分:0)

与多实体管理器一起工作时,我遇到了同样的问题。 像这样的异常 在链中找不到AuthenticationServiceException App \ Entity \ User'。.

此例外仅在生产环境中存在。

我通过添加default_entity_manager :

解决了该问题
# config/packages/prod/doctrine.yaml
doctrine:
    orm:
        default_entity_manager: 'your default entity manager name'

# ...