我一直在使用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>
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
/**
* 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.');
}
答案 0 :(得分:1)
您可以在KernelEvents::REQUEST
上添加优先级为9的事件订阅者,因为类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]
];
}
}
答案 1 :(得分:0)
这是一个例子 - 魔术瘦身是&#34;身份验证事件&#34;。还请看一下 - &gt; https://symfony.com/doc/current/components/security/authentication.html
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters:
# parameter_name: value
account.login_listener.class: AppBundle\Listener\LoginListener
services:
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
# Listeners
account.login_listener:
class: "%account.login_listener.class%"
arguments: ["@security.token_storage"]
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onLogin }
然后你必须定义这样的服务(app / config / services.yml):
{{1}}
答案 2 :(得分:-2)
我不明白为什么你没有使用FormType以正确的方式使用登录表单,你可以轻松处理那里的recaptcha包。
LoginType.php
namespace AppBundle\Form\Type;
use \EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
class LoginType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('username', \Symfony\Component\Form\Extension\Core\Type\TextType::class, array(
))
->add('password', \Symfony\Component\Form\Extension\Core\Type\PasswordType::class, array(
))
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image'
)
),
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
),
'label' => false
));
}
}
调用if ($form->isValid() && $form->isSubmitted()) {
后,它还会检查验证码是否正确。
简单干净。