首页 > 解决方案 > 未调用身份验证事件

问题描述

我有以下简单的课程:

<?php

namespace App\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class LoginListener implements EventSubscriberInterface
{

    private $logger = null;

    public function __construct (LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function onInteractiveLogin (InteractiveLoginEvent $event)
    {
        $this->logger->info('TEST');
    }

    public static function getSubscribedEvents ()
    {
        return [
          SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
        ];
    }

}

它在/src并且应该自动加载(Symfony 4.1.0)。

当我运行时:

bin/console debug:event-dispatcher security.interactive_login

我看到了预期的结果:

Registered Listeners for "security.interactive_login" Event

 ------- ------------------------------------------------------- ---------- 
  Order   Callable                                                Priority  
 ------- ------------------------------------------------------- ---------- 
  #1      App\EventListener\LoginListener::onInteractiveLogin()   0         
 ------- ------------------------------------------------------- ---------- 

然而,当我成功(或以其他方式)登录时,该日志消息并不存在 - 深入研究 Symfony 分析器 - 我可以看到它位于“未调用的侦听器”选项卡下???

有什么我想念的想法吗?

编辑 |

服务.yaml:

imports:
    - { resource: 'company.yaml' }

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-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.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # 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/{Entity,Migrations,Tests,Legacy}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    #
    # ldap authentication service configuration
    # https://symfony.com/doc/current/reference/configuration/security.html
    Symfony\Component\Ldap\Ldap:
        arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
    Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
        arguments:
            -   host: davinci-1
                port: 389
                #encryption: tls
                options:
                    protocol_version: 3
                    referrals: false

    # 
    # We need to hook into default LDAP authentication to properly populate Symfony
    # roles and / or return legacy groups
    # http://symfony.com/doc/current/components/security/authentication.html#authentication-events
    # http://symfony.com/doc/current/event_dispatcher.html
    #App\EventListener\LoginListener:
    #  tags:
    #    - { name: kernel.event_listener, event: security.interactive_login, method: onInteractiveLogin }

安全.yaml

security:
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        ad_ldap:
            ldap:
                service: Symfony\Component\Ldap\Ldap
                base_dn: dc=company,dc=local
                search_dn: 'appuser'
                search_password: "XXX"
                default_roles: ROLE_USER
                uid_key: 'sAMAccountName'
                filter: '({uid_key}={username})'        
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            http_basic_ldap:
                provider: ad_ldap
                service: Symfony\Component\Ldap\Ldap                                
                dn_string: 'Company\{username}'  
                #query_string: '(sAMAccountName={username})'

    # 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 }

标签: symfonysymfony4

解决方案


推荐阅读