4

I need to execute a function before each def in the controller is invoked (before each new request is sent to the server). In this function I will 1) do a session check if valid forward the request to the appropriate action & controller with the params 2) do a sanitization check for user submitted params

I am aware of per controller interceptor, how can I write a global common interceptor.

how and where can I invoke this function in grails? Thanks in advance..

pri_dev
  • 11,315
  • 15
  • 70
  • 122

3 Answers3

6

You can use beforeInterceptor in the Controller for exactly the same purpose. Put this into a new controller say BaseController and put in all the stuff which you want to do and then extend all your existing Controllers with BaseController and you are ready to go. For more information on beforeInterceptor Click here

Note: If you already have some common controller which is extended by all other controllers, no need to implement a new controller. Just implement the beforeInterceptor in that controller itself.

Alternatively, you can have a filter implemented and do all your stuff in that filter. For more information on filters in Grails Click Here

Bagira
  • 2,149
  • 4
  • 26
  • 55
  • Thanks Bala, right now I am thinking on the lines of Filters I just read the documentation, but how can I make a filter for all controllers except AppStartupController(all defs) since session wont be available in this controller yet... and then auth def in login controller (the other def in login controller - logout def needs to have session check). How to write this filter? – pri_dev Mar 27 '12 at 02:17
  • You can use the following syntax to exclude certain controllers filter1(controller:'book', invert:true) { }. This would execute the filter1 for all the controller requests except for book controller (BookController). Hope this helps. – Bagira Mar 28 '12 at 01:00
4

You can add filter in following way to intercept each action


class AbcFilters {
 def filters = {
  abc1(controller: '*', action: '*') {
  }

  abc2(controller: '*Test', action: '*') {
  }


  abc2(controllerExclude: 'AppStartup*', action: '*') {

  }
 }
}
Hussain Fakhruddin
  • 3,202
  • 4
  • 25
  • 36
3

Here is a filter for every action in every controller:

all(controller: '*', action: '*') {
    before = {

    }
    after = {

    }
    afterView = {

    }
}

here is a filter for session check that uses spring security:

     auditEntry(controller:'*', action:'*') {
            before = {
                if (session.isNew()){
                    def userName
                    def principal = SCH?.context?.authentication?.principal 
                    if (principal instanceof String){
                        userName = principal
                        log.info "Anonymous user has logged into the application."
                    }else{
                        userName = principal?.username
                        log.info "User $userName has logged into the application."
                        log.debug (principal)
                    }
                }
            }
        }
dbrin
  • 15,525
  • 4
  • 56
  • 83
  • Thanks for the help, I am right now doing this by creating an Abstract Base controller and have all extend it. in this I can have the beforeInterceptor which will call the functions. – pri_dev Mar 28 '12 at 02:45
  • sure OK, i think filters is a cleaner way though. – dbrin Mar 28 '12 at 05:11