嗨,我開始學習 symfony 事件系統并對其進行測驗,我創建了一個“訂閱者”來監聽AuthenticationEvents::AUTHENTICATION_FAILURE事件并將其轉儲。
然后我嘗試連接假資料,但沒有任何反應。
然后我嘗試聽另一個事件KernelEvents::REQUEST并且它起作用了,所以我看不出問題出在哪里。
我的訂閱者:
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


uj5u.com熱心網友回復:
Core\AuthenticationEvents 類是舊身份驗證系統的一部分。對于基于 HTTP 的新系統,事件類名稱用于事件名稱。所以:
public static function getSubscribedEvents(): array
{
return [
LoginSuccessEvent::class => 'onLoginSuccess',
LoginFailureEvent::class => 'onLoginFailure',
];
}
應該讓你更進一步。
查看下面的一些偵聽器類可能也很有啟發性 vendor\symfony\security-http\Authenticator\EventListener
uj5u.com熱心網友回復:
當然,我完全不合邏輯。非常感謝您的回答。
但是訂閱者仍然沒有被呼叫。enable_authenticator_manager: true' and I tried with the function dd()` 還是什么都沒有。
我的 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:
========================================================================================
------- ---------------------------------------------------------------- ----------
Order Callable Priority
------- ---------------------------------------------------------------- ----------
#1 App\EventSubscriber\AuthenticationSubscriber::onLoginFailure() 0
------- ---------------------------------------------------------------- ----------```
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/335655.html
