duanlun1955 2017-09-29 15:06
浏览 70

如何在Symfony会话超时后重定向到登录页面?

My problem, When session timed out, i refresh the page and then i get 500 error:

Impossible to access an attribute ("id") on a null variable.

because "id" is empty.

I want to redirect to login page, when user refreshes the page and his session has expired. How can I do it?

I falowed this How to log users off automatically after a period of inactivity?

I tried to use listener with Kernel action:

I modified TokenStorageInterface to AuthorizationChecker

Because this error:

Type error: Argument 2 passed to GE\CandidatBundle\EventListener\SessionIdleHandler::__construct() must implement interface Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface, instance of Symfony\Component\Security\Core\Authorization\AuthorizationChecker given

<?php

namespace GE\CandidatBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;

class SessionIdleHandler
{
    protected $session;
    protected $securityToken;
    protected $router;
    protected $maxIdleTime;

    public function __construct(SessionInterface $session, AuthorizationChecker $securityToken, RouterInterface $router, $maxIdleTime = 0)
    {
        $this->session = $session;
        $this->securityToken = $securityToken;
        $this->router = $router;
        $this->maxIdleTime = $maxIdleTime;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {

            return;
        }

        if ($this->maxIdleTime > 0) {

            $this->session->start();
            $lapse = time() - $this->session->getMetadataBag()->getLastUsed();

            if ($lapse > $this->maxIdleTime) {

                $this->securityToken->setToken(null);
                $this->session->getFlashBag()->set('info', 'You have been logged out due to inactivity.');

                // Change the route if you are not using FOSUserBundle.
                $event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));
            }
        }
    }
}

In the services.yml: I modified "@security.context" to "@security.authorization_checker"

Because the security.context service is deprecated.

my.handler.session_idle:
    class: GE\CandidatBundle\EventListener\SessionIdleHandler
    arguments: ["@session", "@security.authorization_checker", "@router", %session_max_idle_time%]
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

In the parameters.yml:

parameters:
    ...
    session_max_idle_time: 1800

Now when i refresh the page i get 500 error, it doesn't work.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数