【问题标题】:Symfony 3 captcha bundle without forms没有表格的 Symfony 3 验证码包
【发布时间】:2019-05-13 14:41:17
【问题描述】:

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

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

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

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

欢迎任何建议,谢谢。

【问题讨论】:

  • 试试这个——该指南展示了如何使用普通的 PHP 来做到这一点,你只需要用 Symfony 的方式来实现它。 acmeextension.com/generate-captcha-image-with-php 请记住,这样做非常不安全,建议始终使用行业标准。

标签: php symfony


【解决方案1】:

我曾经这样使用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');
    }
    // ...
}

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多