How to redirect an anonymous user to the login form after a 403 error?
I've seen that this question was never answered how to do this programmatically. The code actually does work, when placed in an Exception Subscriber:

/src/EventSubscriber/RedirectOn403Subscriber.php:

&lt;?php

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;

class RedirectOn403Subscriber extends HttpExceptionSubscriberBase {

  protected $currentUser;

  public function __construct(AccountInterface $current_user) {
    $this-&gt;currentUser = $current_user;
  }

  protected function getHandledFormats() {
    return ['html'];
  }

  public function on403(GetResponseForExceptionEvent $event) {
    $request = $event-&gt;getRequest();
    $is_anonymous = $this-&gt;currentUser-&gt;isAnonymous();
    $route_name = $request-&gt;attributes-&gt;get('_route');
    $is_not_login = $route_name != 'user.login';
    if ($is_anonymous &amp;&amp; $is_not_login) {
      $query = $request-&gt;query-&gt;all();
      $query['destination'] = Url::fromRoute('&lt;current&gt;')-&gt;toString();
      $login_uri = Url::fromRoute('user.login', [], ['query' =&gt; $query])-&gt;toString();
      $returnResponse = new RedirectResponse($login_uri);
      $event-&gt;setResponse($returnResponse);
    }
  }

}


mymodule.services.yml:

services:
  mymodule.exception403.subscriber:
    class: Drupal\mymodule\EventSubscriber\RedirectOn403Subscriber
    tags:
      - { name: event_subscriber }
    arguments: ['@current_user']