duan4369 2017-09-18 08:45
浏览 67
已采纳

Symfony - 预登录事件监听器

I need to do some actions before the login process/at the beginning of the login process. Is there any pre login event or how can I implement it?

I'am using Symfony3 and I have a form login and OAuth login (HWIOAuthBundle).

I tried this, but it doesn't work with OAuth login. Or how can I implement it for OAuth login too?

  • 写回答

1条回答 默认 最新

  • douan4106 2017-09-18 09:28
    关注

    Generally all login stuff in symfony is executed by a listener to the kernel.request event, mostly within the Firewall listener of the Symfony security component. I did not use the HWIOAuthBundle bundle yet, but it seems it also just uses the Symfony Security component.

    If you just want to execute certain actions before the login/authentication happens, you only need to register an event listener to the kernel.request with a higher priority than the Firewall listener.

    You can do that as described in the Symfony docs (Events and Event Listeners). First create your Event listener class in src/AppBundle/EventListener/YourListener.php:

    namespace AppBundle\EventListener;
    
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    
    class YourListener
    {
        public function onKernelRequest(GetResponseEvent $event)
        {
            // do stuff
        }
    }
    

    Then register your service in your app/config/services.yml file:

    services:
        AppBundle\EventListener\YourListener:
            tags:
                - { name: kernel.event_listener, event: kernel.request, priority: 9 }
    

    According to this page the Firewall has a priority of 8. So the priority of your event listener should be higher than that to be executed before the login happens (I used 9 in the example above).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化