2

I was wondering if it is possible to override a bean definition but using the parent bean's properties as is?

I'm using Grails 2.5.0 along with Spring Security Core 2.0-RC4. In the plugin's descriptor file i.e. SpringSecurityCoreGrailsPlugin, a bean authenticationProcessingFilter is registered as:

authenticationProcessingFilter(RequestHolderAuthenticationFilter) {
    authenticationManager = ref('authenticationManager')
    sessionAuthenticationStrategy = ref('sessionAuthenticationStrategy')
    authenticationSuccessHandler = ref('authenticationSuccessHandler')
    authenticationFailureHandler = ref('authenticationFailureHandler')
    rememberMeServices = ref('rememberMeServices')
    authenticationDetailsSource = ref('authenticationDetailsSource')
    requiresAuthenticationRequestMatcher = ref('filterProcessUrlRequestMatcher')
    usernameParameter = conf.apf.usernameParameter // j_username
    passwordParameter = conf.apf.passwordParameter // j_password
    postOnly = conf.apf.postOnly // true
}

Now, I want to use my own authenticationProcessingFilter which does something else before attempting login so I defined a class:

class CustomAuthenticationFilter extends RequestHolderAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        println "Overridden bean"
        return super.attemptAuthentication(request, response)
    }
}

(I'm just extending the parent bean class and for now added a print statement)

Now, I'm trying to use this class to replace the one defined by Spring plugin. So I wrote something like this in my resources.groovy:

beans = {
    authenticationProcessingFilter(CustomAuthenticationFilter)
}

When I fired my Grails app, it starts complaining that authenticationManager must be specified. So I searched for the problem and found some articles but that doesn't helped. (Articles 1, 2, 3 and 4).

So I tried something like this to use the parent attribute:

beans = {
    authenticationProcessingFilter(CustomAuthenticationFilter) { bean ->
        bean.parent = ref('authenticationProcessingFilter')
    }
}

But the above code didn't worked also. I think it's possible via the value="parent" attribute when beans are defined with XML but I'm not able to get that done in Java way.

Do the only option I have is to redefine all the properties in my resources.groovy like:

authenticationProcessingFilter(CustomAuthenticationFilter) {
    authenticationManager = ref('authenticationManager')
    sessionAuthenticationStrategy = ref('sessionAuthenticationStrategy')
    authenticationSuccessHandler = ref('authenticationSuccessHandler')
    authenticationFailureHandler = ref('authenticationFailureHandler')
    // and other properties like so
}

or is there a cleaner way around it?

Community
  • 1
  • 1
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121

0 Answers0