Symfony2中身份验证失败处理程序的访问原则



我正试图通过自定义身份验证处理程序在数据库中写入一些loggin失败信息。我的问题是访问数据库,因为我不知道条令对象可能存储在的哪里

这是我现在的代码:

namespace MyAppFrontBundleController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest as Request;
use SymfonyComponentHttpFoundationRedirectResponse as RedirectResponse;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentSecurityHttpAuthentication as Auth;
use SymfonyComponentSecurityCoreExceptionAuthenticationException as AuthException;
class SecurityHandler implements AuthAuthenticationFailureHandlerInterface
{
    public function onAuthenticationFailure(Request $request, AuthException $token)
    {
        try
        {
            $lastLoginFailure = new DateTime();
            // get database object here
        }
        catch(Exception $ex)
        {
        }
    }
}

有什么想法吗?

将SecurityHandler转换为服务,然后将条令实体管理器注入其中。

http://symfony.com/doc/current/book/service_container.html

  1. 启动命令php-app/console容器:debug
  2. 复制doctrine.orm.entity_manager并粘贴到您的hadler构造函数参数中,如
    [….,@doctory.orm.entity_manager]
  3. 在hadler中使用DoctrineORMEntityManager;

如果您想使用服务,我认为您应该使用Containerware扩展您的类"SecurityHandler",因为您的安全处理程序不是控制器。

class SecurityHandler extend ContainerAware implements AuthAuthenticationFailureHandlerInterface{
    public function onAuthenticationFailure(Request $request, AuthException $token)
    {
        try
        {
        $lastLoginFailure = new DateTime();
        // get database object here
        $doctrine = $this->container->get('doctrine');
        $repository = $doctrine->getRepository('*NAME OF REPO*');
        }
        catch(Exception $ex)
        {
        }
    }
}

最新更新