title: Format exception message thrown by Spring security in filter chain
body: <p>The setup:
  Resteasy + Spring Security on Tomcat.
  Data exchanged between client/server is in JSON format. 
  Have exception mappers to return JSON formatted messages when the app throws any exception.</p>
  
  <p>Goal:
  Always return properly formatted JSON error message from the server whenever something goes wrong.</p>
  
  <p>What is happening:
  The Spring Security layer is invoked prior to a method foo() being invoked by the servlet container. If this layer fails (e.g. wrong user id), exception is thrown even before the servlet is invoked and hence the error is not properly formatted. 
  Any idea how to get around this and return a JSON error in this case?</p>
  
  <p>Thanks.</p>
  
  <p>Portion of my web.xml if at all helpful:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;securityPropagationFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;com.foo.bar.context.servlet.SecurityContextPropagationFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;loggingFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;securityPropagationFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;loggingFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;
          Resteasy
      &lt;/servlet-name&gt;
      &lt;servlet-class&gt;
          org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
      &lt;/servlet-class&gt;
  &lt;/servlet&gt;
  </code></pre>