【问题标题】:Symfony bug with UserInterface and getToken带有 UserInterface 和 getToken 的 Symfony 错误
【发布时间】:2022-01-05 06:36:37
【问题描述】:

嘿嘿!

我目前正在使用 JWT 进行身份验证。 我使用 Symfony 5.3 我想测试我的登录时出错:

<!-- Cannot autowire argument $user of &quot;App\Controller\AuthController::getTokenUser()&quot;: it references interface &quot;Symfony\Component\Security\Core\User\UserInterface&quot; but no such service exists. Did you create a class that implements this interface? (500 Internal Server Error) -->

我已经安装了安全包和我的用户实体:

class User implements UserInterface, PasswordAuthenticatedUserInterface

有人可以帮助我吗?

谢谢

【问题讨论】:

  • 避免在提问中使用图片。考虑更新问题并在错误的相关部分复制/粘贴。

标签: symfony


【解决方案1】:

也许你写过这行:

class User implements UserInterface, PasswordAuthenticatedUserInterface

之前没有创建use 语句?

use Symfony\Component\Security\Core\User\UserInterface;

当您习惯了 IDE 为您执行此操作时,这是一个典型的忽略,并且在特定时刻它不会发生。

如果是这种情况,这也适用于PasswordAuthenticatedUserInterface

【讨论】:

  • 您好,感谢您的回答。但我有:namespace App\Entity; use App\Repository\UserRepository; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; /** * @ORM\Entity(repositoryClass=UserRepository::class) */ class User implements UserInterface, PasswordAuthenticatedUserInterface
  • 检查这个问题,它可能会有所帮助:stackoverflow.com/a/47964557/17089665
  • 我总是报错 /:
【解决方案2】:

错误表明您的 AuthController::getTokenUser() 方法存在问题,而不是您的 User 类。您可以发布该方法的签名吗?

根据您发布的内容,我的猜测是您的方法如下所示: public function getTokenUser(UserInterface $user)...

如果你在那个控制器方法中需要当前的User,你应该注入Symfony\Component\Security\Core\Security,然后像这样调用$security-&gt;getUser()

public function getTokenUser (Security $security)
{
    $user = $security->getUser();
}

【讨论】:

    猜你喜欢
    • 2012-01-17
    • 2016-02-25
    • 2022-11-03
    • 1970-01-01
    • 2020-08-12
    • 2018-07-27
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多