繁体   English   中英

为什么 Symfony 的 5.1 新安全系统没有触发 InteractiveLoginEvent?

[英]Why isn't InteractiveLoginEvent not fired with Symfony's 5.1 new security system?

使用新的 Symfony 5.1 安全系统,我无法触发InteractiveLoginEvent

我按照官方文档(此处此处)中的配置进行操作,系统在以前的安全系统中运行良好。

以下是我的 security.yaml :

security:
    enable_authenticator_manager: true
    
    encoders:
        App\Entity\User:
            algorithm: auto

        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            form_login:
                login_path: login
                check_path: login
            entry_point: form_login
            
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
            logout:
                path: logout

和 UserLocalSuscriber :

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class UserLocaleSubscriber implements EventSubscriberInterface
{
    private $session;

    public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

    public function onInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        if ($user->getLocale() !== null) {
            $this->session->set('_locale', $user->getLocale());
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
        ];
    }
}

如何正确配置? 没有它,一旦用户登录,用户区域设置就不会正确设置。

对于 Symfony 的新安全系统, SecurityEvents::INTERACTIVE_LOGIN已替换为Symfony\\Component\\Security\\Http\\Event\\LoginSuccessEvent

更改您的订阅者以收听以下内容:

use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class UserLocaleSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            LoginSuccessEvent::class => 'onLoginSuccess',
        ];
    }

    public function onLoginSuccess(LoginSuccessEvent $event): void
    {
        //...
    }

}

这些新事件在博客中与新身份验证器系统的公告一起简要提及。

其余的文档尚未更新,新的身份验证系统可能会成为未来 Symfony realese 的默认设置,但现在它仍然是一个实验性功能

在此处输入图片说明

尝试使用此方法onSecurityInteractivelogin()

暂无
暂无

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

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