title: Is it possible to see if a URL / route is protected behind the firewall programatically?
body: <p>I have a situation where I need to run some code if the user tries accessing a protected URL and is <strong>not</strong> authenticated / authorized. </p>
  
  <p>By default, Symfony handles unauthenticated users by redirecting or forwarding the user to a login form. I'd like to prevent this from happening if the request method is POST, and instead echo a JSON object.  </p>
  
  <p>The best way I can think of to handle this is to create a custom listener that listens to the <code>kernel.request</code> event and checks two things:  </p>
  
  <ul>
  <li>checks if the request method is POST   </li>
  <li>checks if the user is fully
  authenticated</li>
  </ul>
  
  <p>If it is a POST request, and the user is not fully authenticated, I would echo a JSON object.</p>
  
  <p>But my listener is (expectedly) firing for all requests - I'd like to limit it to only check if the request is for a URL protected by the firewall. Is it possible to check this programatically?</p>
  
  <p>I also have a nagging suspicion there's a simpler way to go about this, but can't figure it out, so if anyone has any tips, I'd love to hear them :)</p>
  
  <p><b>Edit</b><br>
  @Problematic - The reason for checking only firewalled requests is because I have some requests that aren't firewalled, and if my code is fired, I'll receive the aforementioned JSON object instead of the request's real response. </p>
  
  <p>Right now if I'm not logged in and make a POST request to <code>api/get/something</code> (which is behind the firewall), Symfony returns the HTML describing the login page. Instead, I want to just echo something like <code>{error: 'User is not authorized'}</code>. But I only want this to happen for POST requests.</p>