Php Symfony防火墙登录:如何在前一个会话失效之前访问它
我正在运行一个基于Php Symfony防火墙登录:如何在前一个会话失效之前访问它,php,symfony,session,Php,Symfony,Session,我正在运行一个基于Symfony 2.8的网页,它使用FOSUserBundle。当用户通过登录从网页的公共部分切换到私有部分时,会话无效(PHPSESSIDchanges)。因此,在登录之后,不可能再访问公共部分使用的会话 在Symfony中,我在注销配置中找到了有关invalidate\u会话的信息 虽然在注销时清理会话数据是有意义的,但我不明白在登录时清理会话数据的原因是什么 问题1: 登录时是否有选项防止Symfony使会话无效 即使有改变这种行为的选择,我还是希望保持这种方式(以防止任
Symfony 2.8
的网页,它使用FOSUserBundle
。当用户通过登录从网页的公共部分切换到私有部分时,会话无效(PHPSESSID
changes)。因此,在登录之后,不可能再访问公共部分使用的会话
在Symfony
中,我在注销配置中找到了有关invalidate\u会话的信息
虽然在注销时清理会话数据是有意义的,但我不明白在登录时清理会话数据的原因是什么
问题1:
登录时是否有选项防止Symfony
使会话无效
即使有改变这种行为的选择,我还是希望保持这种方式(以防止任何不可预见的副作用)。这就引出了第二个问题:
问题2:
在公共会话在登录过程中失效之前,是否有任何事件或其他方式可用于访问该会话
php
使用优先级为8的onKernelRequest
处理程序来运行其身份验证方法。因此,我尝试使用我自己的具有更高优先级的onKernelRequest
处理程序首先访问会话,但没有成功。我只能访问新会话
如何解决这个问题?您应该实现一个并订阅事件
SecurityEvents::INTERACTIVE_登录和FOSUserEvents::注册已完成。此时,公共会话尚未失效,您可以从事件中获取用户
namespace AppBundle\EventListener;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
class YourCustomListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onUserAuthentication',
FOSUserEvents::REGISTRATION_COMPLETED => ['onUserRegistration', -10]
];
}
public function onUserAuthentication(InteractiveLoginEvent $event): void
{
$user = $event->getAuthenticationToken()->getUser();
$this->yourFuntionUsingTheSessionHere($user);
}
public function onUserRegistration(FilterUserResponseEvent $event): void
{
$user = $event->getUser();
$this->yourFunctionUsingTheSessionHere($user);
}
private function yourFunctionUsingTheSessionHere(User $user): void
{
// do your thing here
// I don't know if Eventsubscribers are containeraware maybe you need to inject the container or Symfony\Component\HttpFoundation\Session\SessionInterface to have access to the session
}
}
谢谢你的回答。然而,不幸的是,这是不正确的:在这两个事件中,会话ID已经更改/公共会话已经无效。。。