Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony4 Sylius安全:登录表单Google reCaptcha_Symfony4_Recaptcha_Sylius - Fatal编程技术网

Symfony4 Sylius安全:登录表单Google reCaptcha

Symfony4 Sylius安全:登录表单Google reCaptcha,symfony4,recaptcha,sylius,Symfony4,Recaptcha,Sylius,我正在尝试将ReCaptcha添加到Sylius登录表单中,我已经安装并遵循了所有安装说明,但它没有验证,即使captcha字段为空,表单也会被记录,不会抛出错误。 我目前正在使用Sylius v1.7 视图运行良好,如下所示: 我已扩展SecurityLoginType并创建了一个扩展: <?php declare(strict_types=1); namespace App\Form\Extension; use EWZ\Bundle\RecaptchaBundle\Form\T

我正在尝试将ReCaptcha添加到Sylius登录表单中,我已经安装并遵循了所有安装说明,但它没有验证,即使captcha字段为空,表单也会被记录,不会抛出错误。 我目前正在使用Sylius v1.7

视图运行良好,如下所示: 我已扩展SecurityLoginType并创建了一个扩展:

<?php

declare(strict_types=1);

namespace App\Form\Extension;

use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue as RecaptchaTrue;
use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

class SecurityLoginTypeExtension extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('_recaptcha', EWZRecaptchaType::class, [
            'mapped' => false,
            'constraints' => [
                new RecaptchaTrue(),
            ],
            'attr' => [
                'defer' => true,
                'async' => true,
            ],
        ]);
    }

    public static function getExtendedTypes(): iterable
    {
        return [SecurityLoginType::class];
    }
}
并覆盖SyliusUiBundle/Security/_login.html.twig中的登录tempate

有什么建议吗。
提前感谢。

最后我找到了一种方法,因为我使用FOS bundle,symfony内部不验证登录表单本身,然后我们需要在处理之前捕获登录表单。 我在KerneleEvents::REQUEST上添加了一个优先级为9的事件事件订阅服务器,因为负责为Symfony firewall注册事件的类Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener具有优先级8

下面是代码,它自己解释:

<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class LoginSubscriber implements EventSubscriberInterface
{

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

    /**
     * @param RequestEvent $event
     */
    public function onLogin(RequestEvent $event)
    {
        // Check for login route name
        if ('sylius_shop_login_check' !== $event->getRequest()->attributes->get('_route')) {
            return;
        }

        // Get the login form
        $loginForm = $this->formFactory->createNamed(null, SecurityLoginType::class);

        $loginForm->handleRequest($event->getRequest());

        if ($loginForm->isSubmitted() && !$loginForm->isValid()) {
            // And here code when the form is invalid
        }
    }
}
我要感谢社区。这篇文章对我很有用:

请参阅文档如何使用recaptcha验证器。确保首先启用lol。还要确保您在登录表单中使用相同的验证组,通常在Sylius中是Sylius,但您没有在AFAICT.emix中设置任何验证组,EWZrecaptuble Bundle已启用,并且它在联系人表单和注册表单上运行良好,但奇怪的是,在登录表单上它没有运行。我尝试将sylius设置为验证组,但结果是一样的,我认为问题可能出在SecurityLoginType和框架使用它的方式上,因为它没有验证任何约束。在我看来,这似乎与Symfony如何在内部验证它有关。我现在无法帮助您,但如果您发现了,您是否愿意向插件库提交PR,以便我们也可以为其他用户提供PR?
...
{% form_theme form '@EWZRecaptcha/Form/ewz_recaptcha_widget.html.twig' %}

{{ form_errors(form._recaptcha) }}
{{ form_widget(form._recaptcha, { 'attr': {
    'options' : {
        'theme': 'light',
        'type': 'image',
        'size': 'normal'
    },
} }) }}
...
<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class LoginSubscriber implements EventSubscriberInterface
{

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

    /**
     * @param RequestEvent $event
     */
    public function onLogin(RequestEvent $event)
    {
        // Check for login route name
        if ('sylius_shop_login_check' !== $event->getRequest()->attributes->get('_route')) {
            return;
        }

        // Get the login form
        $loginForm = $this->formFactory->createNamed(null, SecurityLoginType::class);

        $loginForm->handleRequest($event->getRequest());

        if ($loginForm->isSubmitted() && !$loginForm->isValid()) {
            // And here code when the form is invalid
        }
    }
}