简体   繁体   English

Symfony 5 security.interactive_login 事件未调用

[英]Symfony 5 security.interactive_login event not called

I want to use the security.interactive_login event to update my User's last login field.我想使用security.interactive_login事件来更新我的用户的最后登录字段。

The event is successfully registered:活动注册成功:

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
 ------- ------------------------------------------------------------------------ ----------

But it lands on Not called listeners in the Symfony profiler.但它落在 Symfony 分析器中的Not called listeners上。

This is the event subscriber:这是事件订阅者:

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',
        ];
    }
}

And there is my security.yaml file:还有我的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 }

The LoginAuthenticator class is Symfony's default generated one. LoginAuthenticator class 是 Symfony 默认生成的。

Why the interactive login event is not called?为什么不调用交互式登录事件?

When using the new(ish) authentication manager, the INTERACTIVE_LOGIN event is replaced with the LoginSuccessEvent.使用新的(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);
    }

I'm not sure if this is explicitly documented yet.我不确定这是否已明确记录。 Like many upgrade depreciations, the code is very confusing.像许多升级折旧一样,代码非常混乱。 I tried to trace through what was happening and quickly got lost (once again) in the Security forest.我试图追踪正在发生的事情,但很快(又一次)在安全森林中迷路了。

Events are talked about here . 这里讨论事件。

I discovered this behavior by creating a fresh 5.1 project, running make:auth and adding a listener for both events.我通过创建一个新的 5.1 项目、运行 make:auth 并为这两个事件添加一个侦听器来发现这种行为。 But I forgot to add enable_authenticator_manager: true to the security config.但是我忘记将 enable_authenticator_manager: true 添加到安全配置中。

So the INTERACTIVE_LOGIN event was fired.所以 INTERACTIVE_LOGIN 事件被触发。 After enabling the new manager, the LoginSuccessEvent was fired.启用新管理器后,会触发 LoginSuccessEvent。 Notice that the new event has some additional helper methods such as getUser.请注意,新事件有一些额外的帮助方法,例如 getUser。 Makes the code a tiny bit cleaner.使代码更干净一点。

Off-topic but I would caution against flushing the entity manager inside of a listener.题外话,但我会警告不要在侦听器内刷新实体管理器。 It can be a bit unpredictable depending on what else is going on.根据发生的其他情况,它可能有点不可预测。 Might consider just getting the database connection and executing a sql update.可能会考虑获取数据库连接并执行 sql 更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM