Php Symfony 2-onSecurityAuthenticationSuccess处理程序在每次页面加载时都会被调用
我已经创建了一个Php Symfony 2-onSecurityAuthenticationSuccess处理程序在每次页面加载时都会被调用,php,symfony,symfony-2.2,Php,Symfony,Symfony 2.2,我已经创建了一个security.authentication.success事件侦听器,它在登录成功时会向日志发送一行代码。现在,每次我加载防火墙后面的页面时,我都会在日志中收到一条成功的登录消息。如果我尝试使用 if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) { $logger->info('Successful login by ' . $
security.authentication.success
事件侦听器,它在登录成功时会向日志发送一行代码。现在,每次我加载防火墙后面的页面时,我都会在日志中收到一条成功的登录消息。如果我尝试使用
if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY'))
{
$logger->info('Successful login by ' . $username);
}
我陷入了一种递归的疯狂状态(xdebug在10000个嵌套调用后抱怨,或者不管我设置的多高)
是否有办法检查用户是否刚刚登录,或者是否正在使用活动会话
注意:我使用的是Symfony 2.2(dev master)您必须使用:
从文件中: 用户登录后会触发security.interactive_登录事件 主动登录您的网站。区分这一点很重要 来自非交互式身份验证方法的操作,例如:
- 基于“记住我”cookie的身份验证
- 基于会话的身份验证
- 使用HTTP基本或HTTP摘要标头进行身份验证
namespace Acme\UserBundle\Listener;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.1.x
// use Symfony\Bundle\DoctrineBundle\Registry as Doctrine; // for Symfony 2.0.x
/**
* Custom login listener.
*/
class LoginListener
{
/** @var \Symfony\Component\Security\Core\SecurityContext */
private $securityContext;
/** @var \Doctrine\ORM\EntityManager */
private $em;
/**
* Constructor
*
* @param SecurityContext $securityContext
* @param Doctrine $doctrine
*/
public function __construct(SecurityContext $securityContext, Doctrine $doctrine)
{
$this->securityContext = $securityContext;
$this->em = $doctrine->getEntityManager();
}
/**
* Do the magic.
*
* @param Event $event
*/
public function onSecurityInteractiveLogin(Event $event)
{
if ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
// user has just logged in
}
if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
// user has logged in using remember_me cookie
}
// do some other magic here
$user = $this->securityContext->getToken()->getUser();
// ...
}
}