title: Grails Servlet Filter Madness: ...ServletRequestAttributes cannot be cast to..GrailsWebRequest
body: <p>I have a Grails backend with a Flex Single Page Application acting as the UI.  For some reason, when I make an AMF request, my app conks out. with the following error (parts truncated for readability)</p>
  
  <pre><code>2014-04-20 21:07:55,572 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver  -      
  ClassCastException occurred when processing request: [POST]   
  /OrlandoGrails/messagebroker/amf
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
  org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest. Stacktrace follows:
  java.lang.ClassCastException:   
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
   org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest...
  2014-04-20 21:07:55,573 [http-bio-8080-exec-6] ERROR errors.GrailsExceptionResolver  - 
  
  Unable to render errors view: 
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
  org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest...
  java.lang.ClassCastException: 
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
  org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
  Servlet.service() for servlet [Spring MVC Dispatcher Servlet] in context with path 
  [/OrlandoGrails] threw exception [Request processing failed; nested exception is 
  org.codehaus.groovy.grails.exceptions.GrailsRuntimeException: 
  
  java.lang.ClassCastException: 
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
  org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest] with root cause...
  java.lang.ClassCastException: 
  org.springframework.web.context.request.ServletRequestAttributes cannot be cast to 
  org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest at 
   grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53
  </code></pre>
  
  <p>I've read some of the other solutions on the web, and they say that I have to order my web.xml with my springSecurityFilter first and it's mapping last.  I have done that, and below is my web.xml template (before Grails gets ahold of it)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0"
       metadata-complete="true"
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  
  &lt;display-name&gt;/@grails.project.key@&lt;/display-name&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;webAppRootKey&lt;/param-name&gt;
      &lt;param-value&gt;@grails.project.key@&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
      &lt;filter-class&gt;org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;charEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;targetBeanName&lt;/param-name&gt;
          &lt;param-value&gt;characterEncodingFilter&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;targetFilterLifecycle&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;charEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
      &lt;dispatcher&gt;ERROR&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;grails&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;dispatchOptionsRequest&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;async-supported&gt;true&lt;/async-supported&gt;
  &lt;/servlet&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;gsp&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.codehaus.groovy.grails.web.pages.GroovyPagesServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;gsp&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.gsp&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;session-config&gt;
      &lt;session-timeout&gt;30&lt;/session-timeout&gt;
  &lt;/session-config&gt;
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.gsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;flex.messaging.HttpFlexSession&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/config/web-application-config.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/messagebroker/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>The url that is being accessed is  /blah/messagebroker/amf, so I would have thought that only the blazeds messagebroker handler would get ahold of the request, but clearly I am wrong. </p>
  
  <p>Any help on this is greatly appreciated.</p>