【发布时间】:2021-08-02 14:56:37
【问题描述】:
Symfony 5.3
security.yaml
security:
...
erase_credentials: false
LoginListener.php
<?php
namespace App\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class LoginListener
{
private $passwordHasherFactory;
private $em;
public function __construct(PasswordHasherFactoryInterface $passwordHasherFactory, EntityManagerInterface $em)
{
$this->passwordHasherFactory = $passwordHasherFactory;
$this->em = $em;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
$token = $event->getAuthenticationToken();
// Migrate the user to the new hashing algorithm if is using the legacy one
if ($user->hasLegacyPassword()) {
// Credentials can be retrieved thanks to the false value of
// the erase_credentials parameter in security.yml
$plainPassword = $token->getCredentials();
file_put_contents('darius.txt', 'test'.$plainPassword, FILE_APPEND); // why null?
}
$token->eraseCredentials();
}
}
https://symfony.com/doc/current/reference/configuration/security.html#erase-credentials
如果为真,则调用用户对象的 eraseCredentials() 方法 认证后。
所以如果为假,它不应该被删除?为什么要擦除? 收到密码是因为登录有效。我只是在某个时候消失了。
更新
问题是为什么在调用之前凭据为空
$token->eraseCredentials();
【问题讨论】:
-
您自己在 LoginListener 中调用了 eraseCredentials。这是故意的吗?
-
我也对此感到困惑,但我认为问题是为什么
$plainPassword是null。那是在eraseCredentials()之前。 -
@dbrumann - 但它是在 file_put_contents 调用之后,所以仍然应该有密码
标签: symfony