我正在运行一个基于Symfony 2.8
的网页,该网页使用了FOSUserBundle
。当用户通过登录从网页的公共部分切换到私有部分时,会话无效(PHPSESSID
更改)。因此,登录后再也无法访问公共部分上使用的会话。
在Symfony
docs中,我在注销配置中找到了有关invalidate_session
的信息。
虽然注销时清理会话数据很有意义,但我不知道登录时为什么要清除会话数据。
问题1:
是否可以防止Symfony
登录时使会话无效?
即使有更改此行为的选项,我还是希望保持这种方式(以防止任何无法预料的副作用)。这带给我们第二个问题:
问题2: 在登录过程中使公共会话失效之前,是否有任何事件或其他方式可用于访问公共会话?
Firewall.php
使用优先级为8的onKernelRequest
处理程序来运行其身份验证方法。因此,我尝试使用具有更高优先级的自己的onKernelRequest
处理程序来首先访问会话,但这没有解决。我只能访问新会话。
如何解决这个问题?
答案 0 :(得分:0)
您应该实现EventSubscriber并订阅事件 SecurityEvents :: INTERACTIVE_LOGIN和FOSUserEvents :: REGISTRATION_COMPLETED。届时公开会议尚未失效,您可以从事件中吸引用户。
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
}
}