我一直在使用 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/