title: Symfony2 FOS, redirect when not logged in
body: <p>I'd like to redirect user to login page when nobody is not logged in. I wrote lister to that, but all the time I'm getting <code>ERR_TOO_MANY_REDIRECTS</code> error.
  Maybe is another method to achieve it globaly, and redirect user. Checking if user is logged in every controller is not a solution.</p>
  
  <p><strong>Listener\AccessListener.php</strong></p>
  
  <pre><code>namespace Main\UserBundle\Listener;
  
  use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\Security\Core\SecurityContext;
  
  class AccessListener{
  
      private $security;
      private $router;
  
      public function __construct($security, $router){
          $this-&gt;security = $security;
          $this-&gt;router = $router;
      }
  
      public function onKernelRequest(GetResponseEvent $event){
          if ($event-&gt;isMasterRequest()) {
              if( $this-&gt;security-&gt;isGranted('IS_AUTHENTICATED_REMEMBERED') ){
                  $url = $this-&gt;router-&gt;generate('fos_user_security_login');
                  $event-&gt;setResponse(new RedirectResponse($url));
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>security.yml</strong></p>
  
  <pre><code>security:
      access_denied_url: /login
      encoders:
          FOS\UserBundle\Model\UserInterface: sha512
  
      role_hierarchy:
          ROLE_ADMIN:       ROLE_USER
          ROLE_SUPER_ADMIN: ROLE_ADMIN
  
      providers:
          fos_userbundle:
              id: fos_user.user_provider.username
  
      firewalls:
          main:
              pattern: ^/
              form_login:
                  provider: fos_userbundle
                  csrf_provider: form.csrf_provider
                  always_use_default_target_path: false
                  default_target_path:            /
              logout:       true
              anonymous:    true
  
      access_control:
          - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
          - { path: ^/admin/, role: ROLE_ADMIN }
  </code></pre>
  
  <p>How do I correct it?</p>