Symfony的4:注销一个用户自定义控制器

问题描述 投票:0回答:1

我按照教程,使5月的应用程序能够简单地通过调用(如官方文档中描述的通过安全模块)如/注销的路线注销用户。有用。

现在,我想注销用户(通过在文档中描述的仍然登录“记住我”功能),在我自己的控制器(例如电子邮件验证之前,如果另一个会话已通过其他帐户仍然打开)。但没有我的方法工作,它让我疯了。我曾尝试$会话级>明确(),$会话级>无效(),$请求 - > getSession->明确(),$请求 - > getSession->的Invalidate(),等等,等等没有什么工作。

所以我的问题是,请:你怎么办呢?我应该如何处理这种情况?这是否与“记住我”的功能(也许它在其他饼干什么管理?)?

提前致谢

php symfony
1个回答
0
投票

你的猜测也许是正确的,这个问题可以进行相关的记得我的功能,这将使用Cookie存储的令牌,而不是会议,因此需要不同的LogoutHandler。

Symfony的提供多种方式来处理身份验证,您需要根据您的当前设置正确LogoutHandler(S)。

解决您的问题是出奇的难,如果你不就是想将用户重定向到注销路径。 “最好”的办法,我现在能想到的,是由人工建造的请求对象,然后分派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%肯定这将工作,但是这是最接近我能想出一个可能的解决您的问题。你可能要重新考虑自己在做什么,并找到一种替代方法。

© www.soinside.com 2019 - 2024. All rights reserved.