php - Symfony 4 : Login not working for token, 但适用于用户名和密码

标签 php symfony authentication token

我的问题跟踪是:

Type error: Argument 4 passed to

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, null given, called in ...\vendor\symfony\security\Core\Authentication\Provider\UserAuthenticationProvider.php on line 90

事情是:

  • 当我尝试登录时,用户名和密码起作用,但 token 不起作用,并且在我的实体中, token 被定义为参加的数组
/**
* @ORM\Column(type="array")
*/
private $role;

getter 和 setter 相同,

所以,如果我理解得很好,它会尝试访问构造 Authentication 类的数组,这是角色,但它说角色为空。 这是我的登录类:

$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();

if ($this->getUser()) {
        $this->addFlash('danger', 'You are already identified on TCELab');
        return $this->redirectToRoute("default");
    }

return $this->render('user/sign_in.html.twig', array(
        'last_username' => $lastUsername,
        'error' => $error,
    ));

通常这应该有效。我怎样才能做到这一点?

最佳答案

在您的用户实体中,为您的roles 属性设置默认值。

/**
 * @ORM\Column(type="array")
 */
private $roles = [];

public function __construct()
{
    $this->roles = [];
}

关于php - Symfony 4 : Login not working for token, 但适用于用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50603211/

相关文章:

PHP Dom 文档 html 更快还是 preg_match_all 函数更快?

php - 基数违规 : 1222 The used SELECT statements have a different number of columns

forms - 交响乐 3 : Build forms for entities based on Traits

symfony - 如何在 Symfony 2 中进行可选的跨捆绑关联?

windows - 强制用户使用 Windows 服务登录

php - 在 null 上调用成员函数 isCollection()

php - 我可以在 PHP 中混合使用 MySQL API 吗?

php - Doctrine 收集条件 isNull

javascript - Sveltekit & Supabase如何实现cookie认证?

python-3.x - 谷歌Colab : API error in opening up Python notebooks?