【问题标题】:How to remotely clear remember_me cookie for another user in Symfony2?如何远程清除 Symfony2 中另一个用户的 remember_me cookie?
【发布时间】:2016-05-29 08:51:16
【问题描述】:

所以我有这个基本的网络应用程序,它有一个受保护的后台办公区域,只有一个用户(我)可以访问。我希望能够在其他设备(另一台计算机、我的手机等)上远程注销我的会话。

这样,我实现了一个session 表:

CREATE TABLE IF NOT EXISTS `session` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `session_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `login_date` datetime NOT NULL,
  `user_agent` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `ip` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_D044D5D4844A19ED` (`session_token`),
  KEY `IDX_D044D5D4A76ED395` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=15 ;

连同适当的处理程序:

登录监听器

<?php

namespace AppBundle\Event\Listener;

// use ...

class LoginListener implements EventSubscriberInterface
{
    protected $em;

    function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

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

    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        $currentSession = $event->getRequest()->getSession();


        $session = (new \AppBundle\Entity\Admin\Session)
            ->setUser($user)
            ->setSessionToken($currentSession->getId())
        ;

        $this->em->persist($session);
        $this->em->flush();
    }
}

注销监听器

<?php

namespace AppBundle\Event\Listener;

// use ...

class LogoutListener implements LogoutSuccessHandlerInterface {

    private $tokenStorage;
    private $router;
    private $em;

    public function __construct(TokenStorage $tokenStorage, Router $router, EntityManager $em)
    {
        $this->tokenStorage = $tokenStorage;
        $this->router = $router;
        $this->em = $em;
    }

    public function onLogoutSuccess(Request $request)
    {
        $session = $this
            ->em
            ->getRepository('AppBundle:Admin\Session')
            ->findOneBySessionToken($request->getSession()->getId());

        $this->em->remove($session);
        $this->em->flush();

        $response = new RedirectResponse($this->router->generate('blog_home'));
        $response->headers->clearCookie('remember_me');
        $response->headers->clearCookie('session');
        $response->send();

        $request->getSession()->invalidate();

        return $response;
    }
}

这在处理登录和注销时效果很好。该记录在登录时正确插入,并在注销时删除。现在,我可以执行以下操作来远程使会话无效:

public function clearSessionsAction(Request $request)
{
    $session = new Session();
    session_id('<whatever remote session id found in session table>');
    $session->invalidate();

    return $this->redirect($this->generateUrl('blog_home'));
}

但是,如果我在登录时选中了“记住我”以进行远程会话,这将不起作用。

因此,我的问题是:是否可以为当前用户以外的其他用户清除“remember_me”cookie?

当然,我尝试$response-&gt;headers-&gt;clearCookie('remember_me');,但这会结束当前会话。

【问题讨论】:

    标签: php symfony session cookies remember-me


    【解决方案1】:

    您无法真正清除另一个会话的 cookie,因为该会话的浏览器必须先发送请求,然后才能告诉它删除 cookie。无论如何,这是一种不安全的注销方式,因为任何有权访问 cookie 的人都可以手动重新添加它并重用会话。

    实现这一点的正确方法是使用PdoSessionHandler 将会话实际存储在 SQL 数据库中,然后如果要远程注销用户,只需删除数据库中的会话即可。

    【讨论】:

    • 即使启用了remember_me cookie,是否可以使用PdoSessionHandler 远程注销其他会话?
    • @D4V1D 是的。还有一些单词,因为 cmets 必须是 15 个字符。
    • 谢谢!如果我成功了,我会试一试,并接受你的回答并回复你:)
    • 很遗憾,我尝试删除sessions 表中的记录,但是当我刷新页面时,我仍然处于登录状态,并且在表中重新创建了会话记录。
    • RememberMeToken 拥有自动登录的所有信息,这就是为什么使会话无效是不够的
    猜你喜欢
    • 1970-01-01
    • 2019-07-31
    • 2016-06-07
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2017-07-15
    相关资源
    最近更新 更多