How can i set this redirect only for anonymous users? namespace Drupal\project_extras\EventSubscriber;

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

class RedirectAnonymousSubscriber implements EventSubscriberInterface {


  public static function getSubscribedEvents() {
    // This announces which events you want to subscribe to.
    // We only need the request event for this example.  Pass
    // this an array of method names
    return([
      KernelEvents::REQUEST =&gt; [
        ['redirectMyContentTypeNode'],
      ]
    ]);
  }


  /**
   * Redirect requests for my_content_type node detail pages to node/123.
   *
   * @param GetResponseEvent $event
   * @return void
   */

  public function redirectMyContentTypeNode(GetResponseEvent $event) {

    $request = $event-&gt;getRequest();

    // This is necessary because this also gets called on
    // node sub-tabs such as "edit", "revisions", etc.  This
    // prevents those pages from redirected.
    if ($request-&gt;attributes-&gt;get('_route') !== 'entity.node.canonical') {
      return;
    }

    // Only redirect a certain content type.
    if ($request-&gt;attributes-&gt;get('node')-&gt;getType() !== 'specific_content_type') {
      return;
    }

    // This is where you set the destination.
    $redirect_url = Url::fromUri('entity:node/10');
    $response = new RedirectResponse($redirect_url-&gt;toString(), 301);
    $event-&gt;setResponse($response);
  }
}
You can load a user and check for authentication

$currentUser = \Drupal::currentUser();

// Do nothing if the user is authenticated.
if ($currentUser-&gt;isAuthenticated()) {
  return;
}