简体   繁体   中英

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

With the new Symfony 5.1 security system, I'm not able to fire the InteractiveLoginEvent .

I followed the configuration in the official documentation ( here and here ) and the system was working perfectly in the former security system.

Below is my 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

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

How to configure it correctly? Without it, the user locale is not set properly once the user has been logged in.

For the Symfony's new security system, the SecurityEvents::INTERACTIVE_LOGIN has been replaced with Symfony\\Component\\Security\\Http\\Event\\LoginSuccessEvent .

Change your subscriber to listen for this one:

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
    {
        //...
    }

}

These new events are mentioned briefly in the blog with the announcement for the new authenticator system.

The rest of the documentation has not been updated yet, the new auth system will probably become the default on a future Symfony realese, but right now it still is an experimental feature .

在此处输入图片说明

尝试使用此方法onSecurityInteractivelogin()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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