title: Symfony2 @Security Annotation: Turn off redirect when not logged in
body: <p>I've written an API that returns JSON. Certain routes are secured by a @Security annotation on the Controller Action. </p>
  
  <p>If the is_granted() method fails, I catch the thrown exception and output some error json with an 403 http status code.</p>
  
  <p>This works, but only if the user is logged in, but hasn't enough rights.
  If not logged in, the user is redirected to the login page (not useful at all in an ajax call).</p>
  
  <p>What can I do to prevent that redirect?</p>
  
  <p>I've tried to add following line to the security.yml access_control section, but with no effect:</p>
  
  <pre><code>access_control:
      - { path: ^/api, role: IS_AUTHENTICATED_ANONYMOUSLY }
  </code></pre>