gpt4 book ai didi

php - 在检查用户验证以检查 reCaptcha 之前 - Symfony2

转载 作者:行者123 更新时间:2023-12-02 05:17:22 24 4
gpt4 key购买 nike

我想检查验证码 Google reCaptcha在验证用户和密码之前在处理程序中。

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,我想在使用处理程序验证或扩展某些 Controller 之前检查验证码是否正确。

我的问题是,我可以使用什么处理程序来执行此操作?和以前一样登录?

最佳答案

我看到这是一个老问题,但这是我所做的,以防对某人有所帮助。

您想创建一个自定义的 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);
}
}

您需要在 Bundle 配置或应用配置中注册您的身份验证监听器。

YAML 示例:

parameters:
security.authentication.listener.form.class: Acme\UserBundle\EventListener\FormAuthenticationListener

由于您覆盖了 UsernamePasswordFormAuthenticationListener,请不要忘记在自定义逻辑之后以 return parent::attemptAuthentication($request) 结束该方法

关于php - 在检查用户验证以检查 reCaptcha 之前 - Symfony2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com