1

使用新的 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',
        ];
    }
}

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

4

2 回答 2

2

对于 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 的默认设置,但目前它仍然是一个实验性功能

在此处输入图像描述

于 2020-11-11T10:56:19.343 回答
0

尝试使用此方法onSecurityInteractivelogin()

于 2020-12-31T14:04:29.807 回答