douyiavxxh02727 2015-07-29 15:58
浏览 22
已采纳

在Symfony2中以编程方式登录自定义实体用户后,“Authenticated No”

Programmatically logging in the user through:

$user = new CustomEntityUser();
$user->setUsername("foo");
$user->setPassword("bar");

$entityManager->persist($user);
$entityManager->flush();

$token = new UsernamePasswordToken($user, $user->getPassword(), "main", $user->getRoles());
$this->get('security.token_storage')->setToken($token);

doesn't authenticate the user properly. The debug bar goes yellow and says: "Authenticated No". The name of the firewall is main.

The user-entity-class has implemented the Serializeable-interface according to documentation.

  • 写回答

1条回答 默认 最新

  • doushou9028 2015-07-29 15:58
    关注

    The solution was to have the user-entity-class implement the Symfony\Component\Security\Core\User\EquatableInterface, such as e.g.:

    /**
     * {@inheritdoc}
     */
    public function isEqualTo(UserInterface $user)
    {
        if (! $user instanceof User) {
            return false;
        }
    
        if ($this->username !== $user->getUsername()) {
            return false;
        }
    
        if ($this->password !== $user->getPassword()) {
            return false;
        }
    
        return true;
    }
    

    After doing this, the user is successfully authenticated.

    If above fix doesn't work, ensure that the data is properly validated. E.g:

    /**
     * @var string
     *
     * @ORM\Column(name="username", type="string", length=40, unique=true)
     */
    private $username;
    

    and then generating a username with more than 40 characters will still give the same problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题