【问题标题】:The class 'App\Entity\User' was not found in the chain configured namespaces在链配置的命名空间中找不到类“App\Entity\User”
【发布时间】:2019-08-10 12:01:55
【问题描述】:

我遇到了错误:

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

错误发生在这里:

return $userProvider->loadUserByUsername($apiKey);

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

【问题讨论】:

    标签: authentication symfony-4.2


    【解决方案1】:

    我在使用多实体管理器时遇到了同样的问题。 像这样的例外 AuthenticationServiceException App\Entity\User' 未在链中找到..

    此异常仅在生产环境中。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 2019-08-23
      • 1970-01-01
      • 2021-12-27
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多