8.5.3 Ordering Interceptor Execution

Interceptors can be ordered by defining an order property that defines a priority.

For example:

  1. class AuthInterceptor {
  2. int order = HIGHEST_PRECEDENCE
  3. ...
  4. }

The default value of the order property is 0. Interceptor execution order is determined by sorting the order property in an ascending direction and executing the lowest numerically ordered interceptor first.

The values HIGHEST_PRECEDENCE and LOWEST_PRECEDENCE can be used to define filters that should should run first or last respectively.

Note that if you write an interceptor that is to be used by others it is better increment or decrement the HIGHEST_PRECEDENCE and LOWEST_PRECEDENCE to allow other interceptors to be inserted before or after the interceptor you are authoring:

  1. int order = HIGHEST_PRECEDENCE + 50
  2. // or
  3. int order = LOWEST_PRECEDENCE - 50

To find out the computed order of interceptors you can add a debug logger to logback.groovy as follows:

  1. logger 'grails.artefact.Interceptor', DEBUG, ['STDOUT'], false

You can override any interceptors default order by using bean override configuration in grails-app/conf/application.yml:

  1. beans:
  2. authInterceptor:
  3. order: 50

Or in grails-app/conf/application.groovy:

  1. beans {
  2. authInterceptor {
  3. order = 50
  4. }
  5. }

Thus giving you complete control over interceptor execution order.