简体   繁体   中英

Symfony 3 captcha bundle without forms

I need to add a simple captcha to my Symfony login form and currently I am searching for correct bundle for it. I dont need any API/ajax js support, just a bundle which generates an image on the server and then performs user input validation.

Moreover I am not using forms in my project, so I need to render an image in my loginAction and after that perform manual validation somewhere.

Firstly I tried captcha.com bundle, but as far as I understand it is not free and also it required login to git.captcha.com when performing composer require ...

After that I tried to use Gregwar/CaptchaBundle but its docs contain only examples with form while I need something without them. Is there any way to use Gregwar/CaptchaBundle without forms?

Any advice would be welcome, thank you.

I've used Gregwar/CaptchaBundle like this:

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%'

then validate value in AppBundle\\Security\\FormAuthenticator

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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