繁体   English   中英

没有表格的Symfony 3 captcha bundle

[英]Symfony 3 captcha bundle without forms

我需要在我的Symfony登录表单中添加一个简单的验证码,目前我正在为它搜索正确的包。 我不需要任何API / ajax js支持,只需要一个在服务器上生成图像然后执行用户输入验证的包。

此外,我没有在我的项目中使用表单,所以我需要在loginAction渲染图像,之后在某处执行手动验证。

首先我尝试了captcha.com捆绑,但据我所知,它不是免费的,并且在执行composer require ...时需要登录git.captcha.com composer require ...

之后,我尝试使用Gregwar/CaptchaBundle但其文档仅包含带有表单的示例,而我需要没有它们的内容。 有没有办法在没有表格的情况下使用Gregwar/CaptchaBundle

欢迎任何建议,谢谢。

我像这样用过Gregwar/CaptchaBundle

namespace AppBundle\Security\Captcha;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Templating\EngineInterface;
use Gregwar\CaptchaBundle\Generator\CaptchaGenerator as BaseCaptchaGenerator;

class CaptchaGenerator
{
    /**
     * @var EngineInterface
     */
    private $templating;

    /**
     * @var BaseCaptchaGenerator
     */
    private $generator;

    /**
     * @var Session
     */
    private $session;

    /**
     * @var string
     */
    private $sessionKey;

    /**
     * @var array
     */
    private $options;

    public function __construct(EngineInterface $templating, BaseCaptchaGenerator $generator, SessionInterface $session, $sessionKey, array $options)
    {
        $this->templating = $templating;
        $this->generator = $generator;
        $this->session = $session;
        $this->sessionKey = $sessionKey;
        $this->options = $options;
    }

    /**
     * @return string
     */
    public function generate()
    {
        $options = $this->options;
        $code = $this->generator->getCaptchaCode($options);

        $this->session->set($this->sessionKey, $options['phrase']);

        return $this->templating->render('AppBundle:My/Security:captcha.html.twig', array(
            'code' => $code,
            'width' => $options['width'],
            'height' => $options['height'],
        ));
    }
}
namespace AppBundle\Security\Captcha;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class CaptchaValidator
{
    /**
     * @var Session
     */
    private $session;

    /**
     * @var string
     */
    private $sessionKey;

    public function __construct(SessionInterface $session, $sessionKey)
    {
        $this->session = $session;
        $this->sessionKey = $sessionKey;
    }

    /**
     * @param string $value
     * @return bool
     */
    public function validate($value)
    {
        return $this->session->get($this->sessionKey, null) === $value;
    }
}
{# AppBundle:My/Security:captcha.html.twig #}
<img class="captcha_image" src="{{ code }}" alt="" title="captcha" width="{{ width }}" height="{{ height }}" />
# services.yaml
parameters:
    app.security.captcha.security_key: captcha
services:
    AppBundle\Security\Captcha\CaptchaGenerator:
        lazy: true
        arguments:
            $sessionKey: '%app.security.captcha.security_key%'
            $options: '%gregwar_captcha.config%'

    AppBundle\Security\Captcha\CaptchaValidator:
        arguments:
            $sessionKey: '%app.security.captcha.security_key%'

然后在AppBundle\\Security\\FormAuthenticator验证值

public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
    if (!$this->captchaValidator->validate($token->getCaptchaValue())) {
        throw new CustomUserMessageAuthenticationException('security.captcha');
    }
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM