登录前添加监听器 [英] Add listener before login

查看:87
本文介绍了登录前添加监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用symfony 3创建Web应用程序,已使用EWZRecaptchaBundle将Recaptcha添加到我的登录表单中,如何在登录前添加侦听器以验证Recaptcha的有效性.

I have been creating web application using symfony 3, I have added Recaptcha to my login form using EWZRecaptchaBundle, how I can add a listener before login to verify the validation of Recaptcha.

<form method="post" action="{{ path('mysubmited') }}" id="form-validation" name="form-validation">

 <div class="form-group form-input-icon form-input-icon-right">
 <i class="icmn-spinner11 cat__core__spin"></i>
 <div> {{ form_widget(form.username) }}</div>
 </div>

 <div class="form-group">

 <div>{{ form_widget(form.password) }}</div>
 </div>
 <div class="offset-md-3 col-md-4">
 {% form_theme form 
 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %}
 {{ form_widget(form.recaptcha) }}
 </div>
 <div class="form-actions">
 <button type="submit" class="btn btn-primary">Connexion</button>
 <label class="ml-3">
 <a href="#" class="swal-btn-lost-password"> Mot de passe oublié ?</a>
 </label>
 </div>
 </form>

Security.yml

Security.yml

    form_login:
        check_path: /mysubmited
        login_path: /login
        username_parameter: "login_form[username]"
        password_parameter: "login_form[password]"
        #recaptcha_parameter: "login_form[recaptcha]"
        csrf_parameter: "login_form[_token]"
        csrf_token_id: a_private_string
        provider: my_provider
        default_target_path: homepage-auth 

SecurityController.php

SecurityController.php

   /**
     * Login check action.
     *
     * @Route("/mysubmited", name="mysubmited")
     * @throws \RuntimeException
     */
    public function mysubmitedAction(Request $request)
    {

        throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
    }

推荐答案

您可以在优先级为9的 KernelEvents :: REQUEST 上添加事件订阅者,因为类 Symfony \ Bundle \ SecurityBundle \ Debug\ TraceableFirewallListener (负责为symfony防火墙注册事件)的优先级为8.

You can add event subscriber on KernelEvents::REQUEST with priority 9. because class Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener(responsible for registering the events for symfony firewall) has priority 8.

这是您的活动订阅者:

class LoginSubscriber implements EventSubscriberInterface
{

    /**
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        if ('LOGIN_ROUTE_NAME' !== $event->getRequest()->attributes->get('_route')) {
            return;
        }
        // get recaptcha from request and validate it.if you want to prevent request to call next event, just set response for event. $event->setResponse($response)
    }

    public static function getSubscribedEvents()
    {
        return [
           KernelEvents::REQUEST => ['onKernelRequest', 9]
        ];
    }
}

这篇关于登录前添加监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆