Php 在检查用户验证之前,检查reCaptcha-Symfony2
我想在验证用户和密码之前检查处理程序中的验证码Php 在检查用户验证之前,检查reCaptcha-Symfony2,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,我想在验证用户和密码之前检查处理程序中的验证码 require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_fiel
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
我正在使用FOSUserBundle,我想在使用处理程序或扩展某个控制器进行验证之前检查验证码是否正确
我的问题是,我可以使用什么处理程序来执行此操作?登录之前的内容?您可以覆盖FOSUserBundle登录控制器,并在
登录操作中插入验证码逻辑
在这里,您可以找到如何覆盖控制器:
我知道这是一个老问题,但以下是我所做的,以防对某人有所帮助
您要创建自定义AuthenticationListener:
<?php
namespace Acme\UserBundle\EventListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener;
class FormAuthenticationListener extends UsernamePasswordFormAuthenticationListener
{
protected function attemptAuthentication(Request $request)
{
// check for a valid captcha here
// I am using the GregwarCaptchaBundle
// only shown when throttling in my case
$captcha = $request->request->get('login[captcha]', null, true);
if (null !== $captcha) {
$check = $request->getSession()->get('gcb_captcha');
if ($captcha !== $check['phrase']) {
throw new BadCredentialsException('Captcha is invalid');
}
}
return parent::attemptAuthentication($request);
}
}
由于您重写了UsernamePasswordFormAuthenticationListener,因此不要忘记在自定义逻辑之后使用return parent::attemptAuthentication($request)
结束该方法,只想让您知道有一个包可用于此目的。我使用的GregwarCaptchaBundle
可以在2分钟内安装。谢谢@Patt!我已经安装了,这是真的,它很容易安装,但现在我更喜欢谷歌的reCaptcha。您知道是否存在以前的登录句柄、事件或侦听器吗?谢谢@Ferhad。问题是来自FOSUserBundle的loginAction在登录验证之后执行。我需要在登录验证之前。
parameters:
security.authentication.listener.form.class: Acme\UserBundle\EventListener\FormAuthenticationListener