0

我遇到了错误:

在链配置的命名空间中找不到类“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);
    }

    ...
}

安全.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);

完成后无法加载驱动程序,但我不知道如何解决此问题。谢谢!

4

1 回答 1

0

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

此异常仅在生产环境中。

我通过添加default_entity_manager 解决了这个问题:

# config/packages/prod/doctrine.yaml
doctrine:
    orm:
        default_entity_manager: 'your default entity manager name'

# ...
于 2019-08-24T09:37:35.993 回答