簡體   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