Symfony FOS用户包登录检查操作

时间:2017-11-20 08:07:42

标签: symfony fosuserbundle

覆盖login_check操作。我需要添加一些额外的功能。当用户提交表单时,我需要检查他的角色和用户实体中的一些其他字段。

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SecurityListener
{
    private $securityContext;
    private $router;
    private $session;

    public function __construct($userManager, $securityContext, $session, $router)
    {

        $this->securityContext = $securityContext;
        $this->router = $router;
        $this->session = $session;
    }

    public static function getSubscribedEvents()
    {
        return array(
            SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
        );
    }

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        $this->securityContext->getToken()->setAuthenticated(false);
        $this->securityContext->setToken(NULL);

        return new RedirectResponse('http://stackoverflow.com');
    }

我使用的是活动。我创建了自定义事件监听器。并覆盖onSecurityInteractiveLogin(类LastLoginListener实现EventSubscriberInterface)。

  1. 重定向不起作用。
  2. 如何退出用户?我的方法是否正确?
  3. 2个事件被解雇:1)我的2)默认,为什么?
  4. 当我删除令牌第二个默认事件时,会在登录表单上重定向我,但上次登录日期会在数据库中写入。
  5. 有人能帮助我吗?

0 个答案:

没有答案
相关问题