dongshan8194 2019-05-22 10:45
浏览 132

当用户在Symfony中注销时,如何停止对XHR请求的执行?

Suppose I have opened my project in 2 different window and I logout using one of the window or you can say session is timed-out/expired (any one of the situation). After that in another window I am able to perform XHR requests when session is destroyed.

To over come this I have searched a lot and implemented some of it. I added a event listener but it did not worked.

namespace Webkul\CampusConnect\EventListener;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Webkul\CampusConnect\EventListener\AjaxAuthenticationListener;

class AjaxAuthenticationListener
{

    /**
     * Handles security related exceptions.
     *
     * @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance
     */
    public function onCoreException(GetResponseForExceptionEvent $event)
    {
        dump('saurabh');
        die;
        $exception = $event->getException();
        $request = $event->getRequest();

        if ($request->isXmlHttpRequest()) {
            if ($exception instanceof AuthenticationException || $exception instanceof AccessDeniedException) {
                $event->setResponse(new Response('', 403));
            }
        }
    }
}

Service.yaml

ajax.authentication.listener:
        class: Webkul\CampusConnect\EventListener\AjaxAuthenticationListener
        tags:
          - { name: kernel.event_listener, event: kernel.exception, method: onCoreException, priority: 1000 }

Javascript

$(document).ready(function() {
$(document).ajaxError(function (event, jqXHR) {
if (403 === jqXHR.status) {
window.location.reload();
}
});
});

How can I stop performing XHR request when user is logged-out?

  • 写回答

1条回答 默认 最新

  • douling6469 2019-07-15 07:56
    关注

    Seems that because your priority is set up to 1000, default listener also runs and responses with a redirect to to login page (which ajax sees - https://stackoverflow.com/a/33578947/9358736). When you change your priority to -1000 then it should work (should, because it depends a lot of your application configuration). You can test it by just adding plain PHP code before $event->setResponse:

    http_response_code(403);
    die('Forbidden');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog