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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。