覆盖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)。
有人能帮助我吗?