How to override a plugin class with custom class in groovy grails.

Posted By : Kamaldeep Singh | 28-Jun-2015
Override a plugin class with custom class in groovy grails.

 

As such Grails configuration happens at runtime therefore, each plugin may configure Spring beans as a reference to which beans are configured i.e. refer to the reference guide which describes each of the Grails plugins and which beans they configure. So in order to override existing one, register new beans by configuring them in “grails-app/conf/spring/resources.groovy”. Beans are defined inside a beans property

                beans = {
                    // beans here
                }
            

To implement this, simply write your original class and in braces, define your custom class with complete path.

 

Example: In the following example, I am overriding Spring restAuthenticationFilter class with CustomRestAuthenticationFilter which is kept in com.authentication package

                beans = {
                    restAuthenticationFilter(com.authentication.CustomRestAuthenticationFilter)
                }
            

Now when your application runs, restAuthenticationFilter class will be overriden by CustomRestAuthenticationFilter.

Further if restAuthenticationFilter is using some dependencies or service like credentialsExtractor or authenticationManager, you can define them too by first importing the original ones and then defining their references as:-

                service_name_used_in_custom_class = ref("service_name_used_in_original_class") :-
            

 

Lets have an example:-

                import org.springframework.security.authentication.AuthenticationDetailsSource;
                import org.springframework.security.authentication.AuthenticationManager;
                import org.springframework.security.web.authentication.AuthenticationFailureHandler;
                import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

                import com.odobo.grails.plugin.springsecurity.rest.credentials.CredentialsExtractor;
                import com.odobo.grails.plugin.springsecurity.rest.token.generation.TokenGenerator;
                import com.odobo.grails.plugin.springsecurity.rest.token.storage.TokenStorageService;

                beans = {
                    restAuthenticationFilter(com.authentication.CustomRestAuthenticationFilter) {
                        credentialsExtractor = ref("credentialsExtractor")
                        authenticationManager = ref("authenticationManager")
                        authenticationSuccessHandler = ref("authenticationSuccessHandler")
                        authenticationFailureHandler = ref("authenticationFailureHandler")
                        authenticationDetailsSource = ref("authenticationDetailsSource")
                        tokenGenerator = ref("tokenGenerator")
                        tokenStorageService = ref("tokenStorageService")
                    }
                }
            

So this was all about overriding a grails plugin class with a custom one.

 

About Author

Author Image
Kamaldeep Singh

Kamaldeep is a highly skilled Backend Developer specializing in Java, specifically the Spring framework. He also has extensive knowledge of Javascript and associated frameworks such as Node.js and Express. He possesses a deep understanding of the latest technologies and has hands-on experience with Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as both relational databases like MySQL and PostgreSQL and non-relational databases like MongoDB. He has made significant contributions to various projects, including Viral Nation, ExamWorks, TNIBRO, Biogas engineering, SecureNow - Web Application, FB Messenger Chatbot, Dialogflow Chatbot, and Catalyst. Kamaldeep's expertise allows him to seamlessly integrate different technologies into applications, highlighting his adaptability and innovative mindset. His practical experience and strong technical skills make him an invaluable asset to any team.

Request for Proposal

Name is required

Comment is required

Sending message..