symfony - 登录前添加监听器

标签 symfony authentication listener recaptcha

我一直在使用 symfony 3 创建 Web 应用程序,我已经使用 EWZRecaptchaBundle 将 Recaptcha 添加到我的登录表单,如何在登录前添加一个监听器以验证 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>

安全.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

   /**
     * 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。

这是您的事件订阅者:

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]
        ];
    }
}

关于symfony - 登录前添加监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45173869/

相关文章:

node.js - 在express.js中维护用户登录状态

jquery - 根据窗口宽度和窗口调整大小启用功能

android - Android 上的 PlusOneButton 如何知道用户是否点击了?

php - Symfony2 Monolog 电子邮件只有一些异常(exception)

mongodb - Symfony2 - 文档选择列表和 MongoDB

zend-framework - Zend 框架或 Symfony2

c - 使用用户名/密码凭证在 Linux 上验证远程用户

php - Symfony2 FOSUserBundle 无法更新 Doctrine 数据库模式或清除缓存

wordpress - 集成 WordPress 和 Laravel 用户管理

javascript - 元素上的 JS 大量监听器清理