开发者

How is the invocation sequence of Grails filters defined

I am using filters to handle a开发者_StackOverflowuthentication and some other pre-condition checks for a Grails application. I've run into a situation where it'd be nice to ensure that filter A is always invoked before filter B.

According to the documentation the "filters are executed in the order they are defined" but it is unclear what that definition refers to. I am familiar with how that works for Java EE ServletFilters, where the sequence is declared by the order of corresponding tags in the web.xml, but as the deployment is handled automatically in Grails, I am not really sure where I could influence the order in which the filters are set up.

Is that possible at all in Grails, and if so, how?

Update

If several filters are declared within one class, it's obvious that they'll be executed in the order that they were declared. I am more concerned with filters defined in different classes and the sequence that those classes will be considered in.


Molske is correct that they're executed in the order defined in the class. One exception is that the first 'before' filter that returns false stops processing.

There's also a new configuration option 'dependsOn' that you can use to order different filter classes, i.e. that MyFilters2 runs after MyFilters1. See "6.6.4 Filter Dependencies" at http://grails.org/doc/latest/


class MyFilters{
def dependsOn=[OtherFilters]
    def filters= {
        doSomething(uri:"/*"){
        //logic
        }
    }
}

In the other filter you can write

class OtherFilters{
    def filters={
        doAnotherThing(uri:"/*"){
            before={
            //do other thing
            }
        }
    }
}


class MyFilters {
  def filters = {
    myFilter2(controller:'*', action:'*') {}
    myFilter1(controller:'*', action:'*') {}
  }
}

In the example above, myFilter2 will be executed first, after that, myFilter1 will be executed.

The order the filters are defined in the filters-class, the order they are executed in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜