简体   繁体   English

symfony 5.3 中未调用订阅者

[英]Subscriber not called in symfony 5.3

Hi I'm starting to learn the symfony event system and to test it I created a "subscriber" that listens to the AuthenticationEvents::AUTHENTICATION_FAILURE event and dumps it.嗨,我开始学习 symfony 事件系统并对其进行测试,我创建了一个“订阅者”来监听AuthenticationEvents::AUTHENTICATION_FAILURE事件并将其转储。

I then tried to connect with fake data, but nothing happens.然后我尝试连接假数据,但没有任何反应。

I then tried to listen to another event KernelEvents::REQUEST and it works, so I don't see where the problem can come from.然后我尝试听另一个事件KernelEvents::REQUEST并且它起作用了,所以我看不出问题出在哪里。

MY SUBSCRIBER :我的订阅者:



namespace App\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;




class AuthenticationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            AuthenticationEvents::AUTHENTICATION_SUCCESS => 'securityauthenticationsuccess',
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'securityauthenticationfailure',
            KernelEvents::REQUEST => 'KernelRequest',
            
            
        ];
        
    }

    public function securityauthenticationfailure(LoginFailureEvent $event){
          dump($event);
    }

    public function securityauthenticationsuccess(LoginSuccessEvent $event){
        dump($event);
  }

  public function KernelRequest(RequestEvent $event){
    dump($event);
}
  

}```


MY SERVICE.YAML :

```# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

services:

    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    
    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    
    

未调用侦听器

未调用侦听器 2

The Core\\AuthenticationEvents class is part of the older authentication system. Core\\AuthenticationEvents 类是旧身份验证系统的一部分。 For the new HTTP based system, the event class name is used for the event name.对于基于 HTTP 的新系统,事件类名称用于事件名称。 So:所以:

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

Should get you one step further.应该让你更进一步。

It might also be instructive to look at some of the listener classes under vendor\\symfony\\security-http\\Authenticator\\EventListener查看vendor\\symfony\\security-http\\Authenticator\\EventListener下的一些侦听器类可能也很有启发vendor\\symfony\\security-http\\Authenticator\\EventListener

Ofcourse I was completely illogical.当然,我完全不合逻辑。 Thank you very much for this answer.非常感谢您的回答。

But the subscriber is still not called.但是订阅者仍然没有被调用。 enable_authenticator_manager: true' and I tried with the function dd()` and still nothing. enable_authenticator_manager: true' and I tried with the function dd()`,但仍然没有。

MY SECURITY.YAML:我的 SECURITY.YAML:

security:
    # https://symfony.com/doc/current/security/authenticator_manager.html
    enable_authenticator_manager: true
    # https://symfony.com/doc/current/security.html#c-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
        App\Entity\User:
            algorithm: auto

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            provider: app_user_provider
            custom_authenticator: App\Security\UserAuthenticator
            logout:
                path: app_logout
                # where to redirect after logout
                # target: app_any_route
            

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

PHP bin/console debug:event-dispatcher LoginFailureEvent: PHP bin/console debug:event-dispatcher LoginFailureEvent:

========================================================================================

 ------- ---------------------------------------------------------------- ---------- 
  Order   Callable                                                         Priority  
 ------- ---------------------------------------------------------------- ----------
  #1      App\EventSubscriber\AuthenticationSubscriber::onLoginFailure()   0
 ------- ---------------------------------------------------------------- ----------```

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

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