Symfony,在控制器中强制注销

xoshrz7s  于 2023-03-23  发布在  其他
关注(0)|答案(2)|浏览(139)

我正在使用Symfony 3.4,我想在控制器中的操作结束时注销我的用户。
这就是行动

public function changeUserEmail() {
     /* change the user email */
     /* perform the logout */
     /* choose the route to redirect to */
     return $this->redirectToRoute(/* some route choosen above */);
}

有没有办法用Symfony的方式实现/* perform the logout */?我在文档中没有找到任何东西。我确实想在控制器中注销(不想重定向到注销路径),我想在控制器中选择要重定向的路由。
非常感谢。
版本或Symfony是3.4

4ioopgfo

4ioopgfo1#

答案在这里

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

// ...

public function changeUserEmail(TokenStorageInterface $tokenStorage) {
     /* change the user email */
     $tokenStorage->setToken();
     /* choose the route to redirect to */
     return $this->redirectToRoute(/* some route choosen above */);
}

不需要使所有会话无效,例如,如果一个会话定义了多个防火墙。

goucqfw6

goucqfw62#

在Symphony 6.2中,你可以使用logout()方法:

use Symfony\Bundle\SecurityBundle\Security;

class SecurityController
{
    public function someAction(Security $security): Response
    {
        //  with csrf
        $response = $security->logout();

        // or without csrf
        $response = $security->logout(false);

        // ... return $response (if set) or e.g. redirect to the homepage
    }
}

相关问题