我想知道是否可以覆盖bean定义但是按原样使用父bean的属性?
我使用Grails 2.5.0和Spring Security Core 2.0-RC4。在插件的描述符文件中,即SpringSecurityCoreGrailsPlugin
,bean authenticationProcessingFilter
注册为:
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
}
现在,我想在尝试登录之前使用我自己的authenticationProcessingFilter
做其他事情,所以我定义了一个类:
class CustomAuthenticationFilter extends RequestHolderAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
println "Overridden bean"
return super.attemptAuthentication(request, response)
}
}
(我只是扩展父bean类,现在添加了一个print语句)
现在,我尝试使用此类替换Spring插件定义的类。所以我在resources.groovy
:
beans = {
authenticationProcessingFilter(CustomAuthenticationFilter)
}
当我解雇我的Grails应用时,它开始抱怨authenticationManager must be specified
。所以我搜索了这个问题并发现了一些文章,但这并没有帮助。 (文章1,2,3和4)。
所以我尝试使用parent
属性:
beans = {
authenticationProcessingFilter(CustomAuthenticationFilter) { bean ->
bean.parent = ref('authenticationProcessingFilter')
}
}
但上述代码也没有奏效。我认为当使用XML定义bean时,可以通过value="parent"
属性实现,但我无法以Java方式完成。
我唯一的选择是重新定义resources.groovy
中的所有属性,如:
authenticationProcessingFilter(CustomAuthenticationFilter) {
authenticationManager = ref('authenticationManager')
sessionAuthenticationStrategy = ref('sessionAuthenticationStrategy')
authenticationSuccessHandler = ref('authenticationSuccessHandler')
authenticationFailureHandler = ref('authenticationFailureHandler')
// and other properties like so
}
还是有更清洁的方法吗?