如何在新的 Symfony 6 上强制注销从 controle 登录的用户? (6.0.1版本)
我尝试了 $tokenStorage->setToken($token);但 setToken() 需要 2 个参数:
(public function setToken(string $tokenId, string $token);)
我尝试了
$request->getSession()->invalidate();
,但我的用户始终处于登录状态...
我想注销用户并重定向到另一个路由(不想重定向到注销路由)
谢谢你
我无法使用/注销,因为我在控制器中,有时我必须确保没有用户登录,因为当我进入这条路线时我会进行治疗。
我需要这个:
当我去/验证路线时:
我的服务:
<?php
namespace App\Service;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class SecurityService
{
public function forceLogout(
Request $request,
EventDispatcherInterface $eventDispatcher,
TokenStorageInterface $tokenStorage) : void
{
$logoutEvent = new LogoutEvent($request, $tokenStorage->getToken());
$eventDispatcher->dispatch($logoutEvent);
$tokenStorage->setToken(null);
}
}
这不起作用,我的 $eventDispatcher->dispacth($logoutEvent) 仅在我刷新页面之前、再次登录之后才起作用!
我找到了解决办法:
public function forceLogout() : void
{
$logoutEvent = new LogoutEvent($this->requestStack->getCurrentRequest(), $this->tokenStorage->getToken());
$this->eventDispatcher->dispatch($logoutEvent);
$this->tokenStorage->setToken(null);
$response = new Response();
$response->headers->clearCookie('REMEMBERME');
$response->send();
}
只需重定向到注销路线:
return $this->redirect($this->generateUrl('YourLogoutRouteName'));
从 6.2 开始,我们有一个
Symfony/Bundle/SecurityBundle/Security
辅助类。它具有以编程方式登录/注销的方法。
文档中的示例:
// src/Controller/SecurityController.php
namespace App\Controller\SecurityController;
use App\Security\Authenticator\ExampleAuthenticator;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
class SecurityController
{
public function someAction(Security $security): Response
{
// get the user to be authenticated
$user = ...;
// log the user in on the current firewall
$security->login($user);
// if the firewall has more than one authenticator, you must pass it explicitly
// by using the name of built-in authenticators...
$security->login($user, 'form_login');
// ...or the service id of custom authenticators
$security->login($user, ExampleAuthenticator::class);
// you can also log in on a different firewall...
$security->login($user, 'form_login', 'other_firewall');
// ...and add badges
$security->login($user, 'form_login', 'other_firewall', [(new RememberMeBadge())->enable()]);
// use the redirection logic applied to regular login
$redirectResponse = $security->login($user);
return $redirectResponse;
// or use a custom redirection logic (e.g. redirect users to their account page)
// return new RedirectResponse('...');
}
}
// src/Controller/SecurityController.php
namespace App\Controller\SecurityController;
use Symfony\Bundle\SecurityBundle\Security;
class SecurityController
{
public function someAction(Security $security): Response
{
// logout the user in on the current firewall
$response = $security->logout();
// you can also disable the csrf logout
$response = $security->logout(false);
// ... return $response (if set) or e.g. redirect to the homepage
}
}