title: How to register spring bean in grails that needs a reference to a filter bean
body: <p>I am trying to use spring security oauth (library not grails plugin, there is only an extremely outdated grails plugin).</p>
  
  <p>I want my app to be an OAuth 1.0a provider (not OAuth 2). The OAuth2 part of spring-security-oauth does not seem to have this problem, because it does not require a reference to the filterchain in the provider config.</p>
  
  <p>What i want is to configure it as shown here: <a href="https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth/sparklr/src/main/webapp/WEB-INF/applicationContext.xml" rel="noreferrer">https://github.com/spring-projects/spring-security-oauth/blob/master/samples/oauth/sparklr/src/main/webapp/WEB-INF/applicationContext.xml</a></p>
  
  <p>I translated this to the groovy syntax of grails:</p>
  
  <pre><code>consumerDetails(InMemoryConsumerDetailsService)
  tokenServices(InMemoryProviderTokenServices)
  
  xmlns oauth: "http://www.springframework.org/schema/security/oauth"
  oauth.'consumer-details-service'(id:'consumerDetails') {
      oauth.consumer(name: 'AndroidRegisterApp', key:'testkey', secret:"testkey123", resourceName:'mobileApi', resourceDescription:'Register devices via mobile app')
  }
  
  oauth.provider(
          'consumer-details-service-ref': "consumerDetails",
          'token-services-ref':'tokenServices',
          'request-token-url':'/oauth/request_token',
          'authenticate-token-url':'/oauth/authorize',
          'access-granted-url':'/requestTokenAuthorized',
          'access-token-url':'/oauth/access_token',
          'filter-chain-ref':'springSecurityFilterChainProxy',
          'require10a':'true'
  )
  </code></pre>
  
  <p>The problem is that when OAuthProviderBeanDefinitionParser parses this config during grails app start, the springSecurityFilterChainProxy does not yet exist so it fails here:
  <a href="https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth/src/main/java/org/springframework/security/oauth/config/OAuthProviderBeanDefinitionParser.java#L179" rel="noreferrer">https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth/src/main/java/org/springframework/security/oauth/config/OAuthProviderBeanDefinitionParser.java#L179</a> while calling ConfigUtils.findFilterChain the important line in there is:</p>
  
  <pre><code>parserContext.getRegistry().getBeanDefinition(filterChainRef)
  </code></pre>
  
  <p>which fails because "springSecurityFilterChainProxy" just doesn't exist in the parserContext (I guess because it only gets created later on). I also tried to postpone this initialization, by putting it in the bootstrap code like this:</p>
  
  <pre><code>def initOauthProvider() {
      def bb = new BeanBuilder(grailsApplication.getMainContext())
      bb.beans {
                     // same bean initialization code as above
              }
       }
  </code></pre>
  
  <p>this also fails because here i only have the beans in the parserContext that are part of my definition (it doesn't see any other grails beans)</p>
  
  <p>is there any way i can fix this? I've seen that the <a href="http://grails.org/doc/latest/api/grails/spring/BeanBuilder.html" rel="noreferrer">BeanBuilder</a> can also be initialized with a RuntimeSpringConfiguration object but i haven't found any way how to get this from my grails app.</p>
  
  <p>I am using:</p>
  
  <ul>
  <li>Grails 2.2.4</li>
  <li>spring-security-oauth:1.0.5 with these excludes: 'spring-security-web', 'spring-security-core', 'spring-asm'</li>
  <li>spring-security-core:2.0-RC2 grails plugin</li>
  </ul>