The Spring Security plugin provides a bean named 'springSecurityService' of type grails.plugins.springsecurity.SpringSecurityService. I need to override the getCurrentUser method of this service.
I first tried to do it using extension
class CustomSecurityService extends SpringSecurityService {
    @Override
    Object getCurrentUser() {
        // my implementation uses methods from the parent class
    }
}
To replace the bean defined by the plugin with an instance of the class above I added the following to resources.groovy
springSecurityService(CustomSpringSecurityService)
But this didn't work because none of the dependencies of SpringSecurityService (the class I'm extending) are set so I get NullPointerExceptions. The reason these dependencies are not set is because there's no longer a spring bean of type SpringSecurityService
So, I then turned to delegation:
import grails.plugins.springsecurity.SpringSecurityService as PluginSpringSecurityService
class CustomSpringSecurityService {
    @Autowired @Delegate
    PluginSpringSecurityService pluginSpringSecurityService
    Object getCurrentUser() {
        // my implementation uses methods from pluginSpringSecurityService
    }
}
开发者_如何学PythonI then defined two beans in resources.groovy
springSecurityService(CustomSpringSecurityService)
pluginSpringSecurityService(grails.plugins.springsecurity.SpringSecurityService)
In this second attempt, I again want the bean named 'springSecurityService' to refer to CustomSpringSecurityService, but I also need a bean of type grails.plugins.springsecurity.SpringSecurityService, because my implememtation of getCurrentUser use some other methods of that bean.
However, I again found that the dependencies of pluginSpringSecurityService are not being set. Is there an easier way to override a method of a bean provided by a plugin in a context that is subject to dependency injection?
Go back to subclassing and redefining the bean in resources.groovy, but satisfy the dependencies. They're auto-injected by name but all listed, so add them explicitly to your redefinition:
springSecurityService(CustomSpringSecurityService) {
   authenticationTrustResolver = ref('authenticationTrustResolver')
   grailsApplication = ref('grailsApplication')
   passwordEncoder = ref('passwordEncoder')
   objectDefinitionSource = ref('objectDefinitionSource')
   userDetailsService = ref('userDetailsService')
   userCache = ref('userCache')
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论