EventSubscriber endless redirect I have Drupal 8.5.1 and I am trying to force a redirect for anonymous users to the CAS login page. First I thought that the issue was related with the firing of the checkAuthStatus function below, however, the issue seems related to an endless redirect:

namespace Drupal\my_custom_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this-&gt;account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {
    \Drupal::logger('my_custom_module')-&gt;error('THIS IS A TEST');
    if ($this-&gt;account-&gt;isAnonymous()
        &amp;&amp; \Drupal::routeMatch()-&gt;getRouteName() != 'cas.legacy_login'
        &amp;&amp; \Drupal::routeMatch()-&gt;getRouteName() != 'cas.service') {
      $response = new RedirectResponse('/cas', 301);
      $response-&gt;send();
    }
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['checkAuthStatus', 27];
    return $events;
  }

}


What could be the issue?
Finally I have solved the issue thanks to the comments I received on my question. Here is the solution I found:

namespace Drupal\my_custom_module\EventSubscriber;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event subscriber subscribing to KernelEvents::REQUEST.
 */
class RedirectAnonymousSubscriber implements EventSubscriberInterface {

  public function __construct() {
    $this-&gt;account = \Drupal::currentUser();
  }

  public function checkAuthStatus(GetResponseEvent $event) {
    if ($this-&gt;account-&gt;isAnonymous()
      &amp;&amp; \Drupal::service('path.current')-&gt;getPath() != '/cas'
      &amp;&amp; \Drupal::service('path.current')-&gt;getPath() != '/casservice') {
      $response = new RedirectResponse('/cas', 301);
      $event-&gt;setResponse($response);
    }
    return;
  }

  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ['checkAuthStatus', 100];
    return $events;
  }

}


In short, the issue was related with the path checking and the RedirectResponse object management.