我正在使用 Symfony2,需要将失败的登录尝试保存到数据库中。
我认为我应该使用以下方法:
// Symfony/Component/Security/Http/Autentication/AbstractAuthenticationListener.php
onFailure()
onSuccess()
但我不确定如何从它们内部访问数据库连接。
如何从这些函数中运行数据库插入?
最佳答案
你需要在security.yml中定义你自己的失败处理程序
form_login:
provider: fos_userbundle
login_path: /user/login
csrf_provider: form.csrf_provider
failure_handler: myBundle.login.failure
创建一个服务来处理失败
bundle.login.failure:
class: 'MyBundle\Services\AuthenticationFailureHandler'
arguments: ['@kernel']
然后构建您的失败处理程序:
<?php
namespace MyBundle\Services;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class AuthenticationFailureHandler extends DefaultAuthenticationFailureHandler
{
public function __construct(HttpKernelInterface $httpKernel)
{
$this->httpKernel = $httpKernel;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
// the auth just failed :-(
}
}
为了将尝试保存到数据库中,将你的 Doctrine 管理器注入(inject)到服务中,并从 onFail 方法中持久化尝试。
关于symfony - 在 Symfony2 中保留失败的身份验证登录尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27953712/