src/AppBundle/Listener/CSRFListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Listener;
  3. use ApiBundle\ApiBundle;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. use Symfony\Component\Security\Csrf\CsrfToken;
  9. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  10. use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
  11. use Twig\Environment;
  12. class CSRFListener
  13. {
  14.     private $container;
  15.     private $twig;
  16.     private $csrfTokenManager;
  17.     public function __construct(ContainerInterface $containerCsrfTokenManagerInterface $csrfTokenManagerEnvironment $twig)
  18.     {
  19.         $this->container $container;
  20.         $this->csrfTokenManager $csrfTokenManager;
  21.         $this->twig $twig;
  22.     }
  23.     public function onKernelRequest(RequestEvent $event)
  24.     {
  25.         $request $event->getRequest();
  26.         if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
  27.             return;
  28.         }
  29.         if ('POST' !== $request->getMethod()) {
  30.             return;
  31.         }
  32.         if (=== stripos($request->getPathInfo(), ApiBundle::API_PREFIX)) {
  33.             return;
  34.         }
  35.         if (=== stripos($request->getPathInfo(), '/mapi')) {
  36.             return;
  37.         }
  38.         if (=== stripos($request->getPathInfo(), '/hls')) {
  39.             return;
  40.         }
  41.         if (=== stripos($request->getPathInfo(), '/callback')) {
  42.             return;
  43.         }
  44.         $whiteList $this->container->hasParameter('route_white_list') ? $this->container->getParameter('route_white_list') : [];
  45.         if (in_array($request->getPathInfo(), $whiteList)) {
  46.             return;
  47.         }
  48.         if ($request->isXmlHttpRequest()) {
  49.             $token $request->headers->get('X-CSRF-Token');
  50.         } else {
  51.             $token $request->request->get('_csrf_token''');
  52.         }
  53.         if (=== stripos($request->getPathInfo(), '/admin/app/package_update/2792/begin_upgrade') || === stripos($request->getPathInfo(), '/admin/app/package_update/check/newest/code/TRAININGMAIN')) {
  54.             $storage = new NativeSessionTokenStorage();
  55.             $newToken = new CsrfToken('site'$token);
  56.             if ($newToken->getValue() == $token) {
  57.                 return;
  58.             }
  59.         }
  60.         $request->request->remove('_csrf_token');
  61.         $isTokenValid $this->csrfTokenManager->isTokenValid(new CsrfToken('site'$token));
  62.         if ($isTokenValid) {
  63.             return;
  64.         }
  65.         $response = new Response($this->twig->render('default/message.html.twig', [
  66.             'type' => 'error',
  67.             'message' => $this->container->get('translator')->trans('页面已过期,请重新提交数据!'),
  68.             'goto' => '',
  69.             'duration' => 0,
  70.         ]), 403);
  71.         $event->setResponse($response);
  72.     }
  73. }