Php Symfony 4:注销自定义控制器中的用户

Php Symfony 4:注销自定义控制器中的用户,php,symfony,Php,Symfony,我遵循了教程,通过调用类似/logout的路由(通过官方文档中描述的安全模块),使may应用程序能够注销用户。它起作用了 现在我想在我自己的控制器中注销用户(仍然通过文档“记住我”功能中描述的登录)(例如,在电子邮件验证之前,如果另一个会话仍然在另一个帐户下打开)。 但我的方法都不管用,这让我发疯。我试过$session->clear(),$session->invalidate(),$request->getSession->clear(),$request->getSession->inva

我遵循了教程,通过调用类似/logout的路由(通过官方文档中描述的安全模块),使may应用程序能够注销用户。它起作用了

现在我想在我自己的控制器中注销用户(仍然通过文档“记住我”功能中描述的登录)(例如,在电子邮件验证之前,如果另一个会话仍然在另一个帐户下打开)。 但我的方法都不管用,这让我发疯。我试过$session->clear(),$session->invalidate(),$request->getSession->clear(),$request->getSession->invalidate()等等,但都没用

所以我的问题是:你是怎么做到的?我该怎么处理这个案子?它是否与“记住我”功能相关(可能是在另一个cookie或其他东西中管理的?)


提前感谢

您的猜测可能是正确的,问题可能与“记住我”功能有关,因为这将使用cookies来存储令牌,而不是会话,因此需要不同的LogoutHandler

Symfony提供多种方式来处理身份验证,您将需要正确的LogoutHandler,具体取决于您当前的设置

如果您不只是想将用户重定向到注销路径,那么解决您的问题就非常困难。我现在所能想到的“最佳”方式是,通过手动构建请求对象,然后使用它调度GetResponseEvent来模拟注销请求,从而触发LogoutListener。分派事件可能会产生奇怪的副作用,因此您甚至可能希望直接触发侦听器。它可能看起来像这样:

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class MyController
{
    private $kernel;
    private $logoutListener;

    public function __construct(HttpKernelInterface $kernel, LogoutListenerInterface $logoutListener)
    {
        $this->kernel = $kernel;
        $this->logoutListener = $logoutListener;
    }

    private function customLogout()
    {
        // This needs to be updated whenever the logout path in your security.yaml changes. Probably something you want to improve on.
        $request = Request::create('/logout');

        $event = new GetResponseEvent($this->kernel, $request);

        $this->logoutListener->handle($event);
    }

    public function someAction()
    {
        $this->customLogout();

        // Do whatever you want to do for your request
    }
}

我认为这不是一个好的解决方案,因为干扰安全系统本身就是危险的。直接调用LogoutListener和传递内核也有点不确定。老实说,我甚至不能100%肯定这会起作用,但这是我能想到的最接近你问题的可能解决方案。您可能需要重新思考您正在做的事情,并找到一种替代方法。

您的猜测可能是正确的,问题可能与“记住我”功能有关,因为这将使用cookies来存储令牌,而不是会话,因此需要一个不同的LogoutHandler

Symfony提供多种方式来处理身份验证,您将需要正确的LogoutHandler,具体取决于您当前的设置

如果您不只是想将用户重定向到注销路径,那么解决您的问题就非常困难。我现在所能想到的“最佳”方式是,通过手动构建请求对象,然后使用它调度GetResponseEvent来模拟注销请求,从而触发LogoutListener。分派事件可能会产生奇怪的副作用,因此您甚至可能希望直接触发侦听器。它可能看起来像这样:

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class MyController
{
    private $kernel;
    private $logoutListener;

    public function __construct(HttpKernelInterface $kernel, LogoutListenerInterface $logoutListener)
    {
        $this->kernel = $kernel;
        $this->logoutListener = $logoutListener;
    }

    private function customLogout()
    {
        // This needs to be updated whenever the logout path in your security.yaml changes. Probably something you want to improve on.
        $request = Request::create('/logout');

        $event = new GetResponseEvent($this->kernel, $request);

        $this->logoutListener->handle($event);
    }

    public function someAction()
    {
        $this->customLogout();

        // Do whatever you want to do for your request
    }
}
我认为这不是一个好的解决方案,因为干扰安全系统本身就是危险的。直接调用LogoutListener和传递内核也有点不确定。老实说,我甚至不能100%肯定这会起作用,但这是我能想到的最接近你问题的可能解决方案。你可能想重新思考你正在做什么,并找到一种替代方法