普通网友 2019-05-14 20:43
浏览 105

使用JWT登录后,为什么会出现“Class not exists”错误

I've configured FOSUserBundle and LexikJWTAuthenticationBundle in Symfony4. Earlier I did these things successfully in Symfony 3 and lower, but this is the first time in Symfony4 and I do get a "Class 'Account' does not exist" from the file "vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/MappingException.php" after being logged in through the NelmioApiDoc > Authorize button and trying to get info. The exception is already been thrown before the code in my controller. The stacktrace tells me it goes wrong when the user entity is tried to be read by the authenticationhandler.

I've already tried:

php bin/console doctrine:mapping:info

Result:

Found 4 mapped entities:

 [OK]   App\Entity\AccountRefreshToken
 [OK]   App\Entity\Account
 [OK]   FOS\UserBundle\Model\Group
 [OK]   FOS\UserBundle\Model\User

Verifying my configs: doctrine.yaml:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

fos_user.yaml:

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\Account
    from_email:
        address: "iamanemail@address.ext"
        sender_name: "iamanemail@address.ext"

Account.php:

<?php


namespace App\Entity;


use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;


/**
 * @ORM\Entity
 * @ORM\Table(name="account")
 * @ORM\HasLifecycleCallbacks
 */
class Account extends User implements TwoFactorInterface
{
    ...
}

composer.json

{
   "required": {
       "php": "^7.1.3",
        "ext-json": "*",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "friendsofsymfony/rest-bundle": "^2.5",
        "friendsofsymfony/user-bundle": "~2.0",
        "jms/serializer-bundle": "^3.3",
        "lexik/jwt-authentication-bundle": "^2.6",
        "monolog/monolog": "^2.0@dev",
        "nelmio/api-doc-bundle": "^3.4",
        "nelmio/cors-bundle": "^1.5",
        "scheb/two-factor-bundle": "^4.1",
        "sensio/framework-extra-bundle": "^5.3",
        "symfony/asset": "4.2.*",
        "symfony/console": "4.2.*",
        "symfony/flex": "^1.1",
        "symfony/framework-bundle": "4.2.*",
        "symfony/orm-pack": "^1.0",
        "symfony/swiftmailer-bundle": "^3.2",
        "symfony/translation": "4.2.*",
        "symfony/yaml": "4.2.*"
   }
   ...
   "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    ...
}

I expected to see the response instead of a mapping exception.

Stacktrace:

#0 /vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(24): Doctrine\\Common\\Persistence\\Mapping\\MappingException::nonExistingClass('Account')
#1 /vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(250): Doctrine\\Common\\Persistence\\Mapping\\RuntimeReflectionService->getParentClasses('Account')
#2 /vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(283): Doctrine\\Common\\Persistence\\Mapping\\AbstractClassMetadataFactory->getParentClasses('Account')
#3 /vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(78): Doctrine\\Common\\Persistence\\Mapping\\AbstractClassMetadataFactory->loadMetadata('Account')
#4 /vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(183): Doctrine\\ORM\\Mapping\\ClassMetadataFactory->loadMetadata('Account')
#5 /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(283): Doctrine\\Common\\Persistence\\Mapping\\AbstractClassMetadataFactory->getMetadataFor('Account')
#6 /vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php(45): Doctrine\\ORM\\EntityManager->getClassMetadata('Account')
#7 /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(713): Doctrine\\Bundle\\DoctrineBundle\\Repository\\ContainerRepositoryFactory->getRepository(Object(Doctrine\\ORM\\EntityManager), 'Account')
#8 /vendor/friendsofsymfony/user-bundle/Doctrine/UserManager.php(114): Doctrine\\ORM\\EntityManager->getRepository('Account')
#9 /vendor/friendsofsymfony/user-bundle/Doctrine/UserManager.php(76): FOS\\UserBundle\\Doctrine\\UserManager->getRepository()
#10 /vendor/friendsofsymfony/user-bundle/Model/UserManager.php(58): FOS\\UserBundle\\Doctrine\\UserManager->findUserBy(Array)
#11 /vendor/friendsofsymfony/user-bundle/Security/UserProvider.php(93): FOS\\UserBundle\\Model\\UserManager->findUserByUsername('admin')
#12 /vendor/friendsofsymfony/user-bundle/Security/UserProvider.php(43): FOS\\UserBundle\\Security\\UserProvider->findUser('admin')
#13 /vendor/lexik/jwt-authentication-bundle/Security/Guard/JWTTokenAuthenticator.php(288): FOS\\UserBundle\\Security\\UserProvider->loadUserByUsername('admin')
#14 /vendor/lexik/jwt-authentication-bundle/Security/Guard/JWTTokenAuthenticator.php(156): Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator->loadUser(Object(FOS\\UserBundle\\Security\\UserProvider), Array, 'admin')
#15 /vendor/symfony/security-guard/Provider/GuardAuthenticationProvider.php(102): Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator->getUser(Object(Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Authentication\\Token\\PreAuthenticationJWTUserToken), Object(FOS\\UserBundle\\Security\\UserProvider))
#16 /vendor/symfony/security-guard/Provider/GuardAuthenticationProvider.php(96): Symfony\\Component\\Security\\Guard\\Provider\\GuardAuthenticationProvider->authenticateViaGuard(Object(Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator), Object(Symfony\\Component\\Security\\Guard\\Token\\PreAuthenticationGuardToken))
#17 /vendor/scheb/two-factor-bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php(65): Symfony\\Component\\Security\\Guard\\Provider\\GuardAuthenticationProvider->authenticate(Object(Symfony\\Component\\Security\\Guard\\Token\\PreAuthenticationGuardToken))
#18 /vendor/symfony/security-core/Authentication/AuthenticationProviderManager.php(76): Scheb\\TwoFactorBundle\\Security\\Authentication\\Provider\\AuthenticationProviderDecorator->authenticate(Object(Symfony\\Component\\Security\\Guard\\Token\\PreAuthenticationGuardToken))
#19 /vendor/symfony/security-guard/Firewall/GuardAuthenticationListener.php(130): Symfony\\Component\\Security\\Core\\Authentication\\AuthenticationProviderManager->authenticate(Object(Symfony\\Component\\Security\\Guard\\Token\\PreAuthenticationGuardToken))
#20 /vendor/symfony/security-guard/Firewall/GuardAuthenticationListener.php(82): Symfony\\Component\\Security\\Guard\\Firewall\\GuardAuthenticationListener->executeGuardAuthenticator('api_0', Object(Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Guard\\JWTTokenAuthenticator), Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#21 /vendor/symfony/security-bundle/Debug/WrappedListener.php(46): Symfony\\Component\\Security\\Guard\\Firewall\\GuardAuthenticationListener->handle(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#22 /vendor/symfony/security-bundle/Debug/TraceableFirewallListener.php(35): Symfony\\Bundle\\SecurityBundle\\Debug\\WrappedListener->handle(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#23 /vendor/symfony/security-http/Firewall.php(90): Symfony\\Bundle\\SecurityBundle\\Debug\\TraceableFirewallListener->handleRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent), Object(Generator))
#24 /vendor/symfony/security-bundle/EventListener/FirewallListener.php(48): Symfony\\Component\\Security\\Http\\Firewall->onKernelRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#25 /vendor/symfony/event-dispatcher/Debug/WrappedListener.php(115): Symfony\\Bundle\\SecurityBundle\\EventListener\\FirewallListener->onKernelRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent), 'kernel.request', Object(Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher))
#26 /vendor/symfony/event-dispatcher/EventDispatcher.php(212): Symfony\\Component\\EventDispatcher\\Debug\\WrappedListener->__invoke(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent), 'kernel.request', Object(Symfony\\Component\\HttpKernel\\Debug\\TraceableEventDispatcher))
#27 /vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\\Component\\EventDispatcher\\EventDispatcher->doDispatch(Array, 'kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#28 /vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php(145): Symfony\\Component\\EventDispatcher\\EventDispatcher->dispatch('kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#29 /vendor/symfony/http-kernel/HttpKernel.php(126): Symfony\\Component\\EventDispatcher\\Debug\\TraceableEventDispatcher->dispatch('kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#30 /vendor/symfony/http-kernel/HttpKernel.php(67): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw(Object(Symfony\\Component\\HttpFoundation\\Request), 1)
#31 /vendor/symfony/http-kernel/Kernel.php(198): Symfony\\Component\\HttpKernel\\HttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)
#32 /public/index.php(25): Symfony\\Component\\HttpKernel\\Kernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))
#33 {main}

I think the problem is that instead of Doctrine\\ORM\\EntityManager->getRepository('Account') it should load Doctrine\\ORM\\EntityManager->getRepository('App\Entity\Account'), but I don't know how to change that.

Does anyone of you has experience with this? And hopefully a solution?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?