8.5 Interceptors

Grails provides standalone Interceptors using the create-interceptor command:

  1. $ grails create-interceptor MyInterceptor

The above command will create an Interceptor in the grails-app/controllers directory with the following default contents:

  1. class MyInterceptor {
  2. boolean before() { true }
  3. boolean after() { true }
  4. void afterView() {
  5. // no-op
  6. }
  7. }

Interceptors vs Filters

In versions of Grails prior to Grails 3.0, Grails supported the notion of filters. These are still supported for backwards compatibility but are considered deprecated.

The new interceptors concept in Grails 3.0 is superior in a number of ways, most significantly interceptors can use Groovy’s CompileStatic annotation to optimize performance (something which is often critical as interceptors can be executed for every request.)