Within the body of the filter you can then define one or several of the following interceptor types for the filter:
before
- Executed before the action. Can return false to indicate all future filters and the action should not execute
after
- Executed after an action. Takes a first argument as the view model
afterView
- Executed after view rendering
For example to fulfill the common authentication use case you could define a filter as follows:
class SecurityFilters {
def filters = {
loginCheck(controller:'*', action:'*') {
before = {
if(!session.user && !actionName.equals('login')) {
redirect(action:'login')
return false
}
} }
}
}
Here the
loginCheck
filter uses a
before
interceptor to execute a block of code that checks if a user is in the session and if not redirects to the login action. Note how returning false ensure that the action itself is not executed.