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

问题描述

使用新的Symfony 5.1安全系统,我无法解雇InteractiveLoginEvent

我遵循了官方文档(herehere)中的配置,并且该系统在以前的安全系统中运行良好。

下面是我的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
    {
        //...
    }

}

blog with the announcement中针对新的身份验证器系统简要提到了这些新事件。

文档的其余部分尚未更新,新的auth系统可能会成为将来的Symfony realese的默认系统,但是现在它仍然是实验功能

enter image description here

,

试试这个方法onSecurityInteractivelogin()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...