gpt4 book ai didi

Symfony 5 security.interactive_login 事件未调用

转载 作者:行者123 更新时间:2023-12-04 01:16:34 25 4
gpt4 key购买 nike

我想用security.interactive_login事件来更新我的用户的上次登录字段。
事件注册成功:

php bin/console debug:event-dispatcher security.interactive_login

Registered Listeners for "security.interactive_login" Event
===========================================================

------- ------------------------------------------------------------------------ ----------
Order Callable Priority
------- ------------------------------------------------------------------------ ----------
#1 App\EventSubscriber\UserLocaleSubscriber::onSecurityInteractiveLogin() 0
------- ------------------------------------------------------------------------ ----------
但它落在 Symfony 分析器中的 Not called listeners 上。
这是事件订阅者:
class UserLocaleSubscriber implements EventSubscriberInterface
{
private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
/** @var User $user */
$user = $event->getAuthenticationToken()->getUser();

$user->setLastLoginAt(new DateTime());
$this->em->persist($user);
$this->em->flush();
}

public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
];
}
}
还有我的 security.yaml 文件:
security:
enable_authenticator_manager: true
encoders:
App\Entity\User:
algorithm: auto

providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js|fonts)/
security: false
main:
lazy: true
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginAuthenticator
logout:
path: app_logout
target: app_login # where to redirect after logout
remember_me:
secret: '%kernel.secret%'
lifetime: 604800 # 1 week in seconds
path: /
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN

# Note: Only the *first* access control that matches will be used
access_control:
- { path: ^/(?!login), roles: ROLE_ADMIN }
LoginAuthenticator 类是 Symfony 默认生成的类。
为什么 互动登录事件没有被调用?

最佳答案

使用新的(ish)身份验证管理器时,INTERACTIVE_LOGIN 事件将替换为 LoginSuccessEvent。

# my subscriber
public static function getSubscribedEvents()
{
return [
//SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
LoginSuccessEvent::class => 'onLoginSuccess'
];
}
public function onLoginSuccess(LoginSuccessEvent $event)
{
$user = $event->getUser();
$user->setCount($user->getCount() + 1);
$this->em->flush();
//dd($user);
}
我不确定这是否已明确记录。像许多升级折旧一样,代码非常困惑。我试图追踪正在发生的事情,但很快(又一次)在安全林中迷路了。
大事记 here .
我通过创建一个新的 5.1 项目、运行 make:auth 并为这两个事件添加监听器发现了这种行为。但是我忘了在安全配置中添加 enable_authenticator_manager: true 。
所以 INTERACTIVE_LOGIN 事件被触发。启用新管理器后,LoginSuccessEvent 被触发。请注意,新事件具有一些额外的辅助方法,例如 getUser。使代码更简洁一点。
题外话,但我会警告不要在监听器中刷新实体管理器。根据其他情况,它可能有点不可预测。可能会考虑只获取数据库连接并执行 sql 更新。

关于Symfony 5 security.interactive_login 事件未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63264934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com