Although In most of cases like doing something before each request grails provides the concept of interceptors with in a controller, but if we like to intercept request in more than one controller than grails another functionality comes into picture called Filters.
One of the use case of filters is that if we need to intercept all request except one special case, the we can use one of the filters feature

invert:true 

e.g

def filters = {
    allExceptIndex(controller:"dashboard",action:"index",invert:true) {
        before = {
        }
        after = { Map model ->
        }
        afterView = { Exception e ->
        }
    }
}

In the above case all action will be intercepted except one action called “index” of “dashboard” controller.
Now comes to heading of the blog i.e invocation sequence, filters executed in the order defined in the class filters, But what if there are multiple filter classes in this case grails provide dependsOn property using this we can maintain the order of execution of filters.e.g taken from grails doc

class MyFilters {
    def dependsOn = [MyOtherFilters]
    def filters = {
        checkAwesome(uri: "/*") {
            before = {
                if (request.isAwesome) { // do something awesome }
            }
        }
        checkAwesome2(uri: "/*") {
            before = {
               if (request.isAwesome) { // do something else awesome }
            }
        }
    }
}

and

class MyOtherFilters {
    def filters = {
        makeAwesome(uri: "/*") {
            before = {
                request.isAwesome = true
            }
        }
        doNothing(uri: "/*") {
            before = {
                // do nothing
            }
        }
    }
}

In this case MyFilters dependsOn MyOtherFilters it means first MyOtherFilters will be executed and then MyFilters will be executed. The order of execution will be:

  • MyOtherFilters – makeAwesome
  • MyOtherFilters – doNothing
  • MyFilters – checkAwesome
  • MyFilters – checkAwesome2