How to execute a function before each new request is passed to the controller in grails

I need to execute a function before calling each def in the controller (before each new request is sent to the server). In this function, I will 1) perform a session check if the valid one redirects the request for the appropriate action and the controller with the parameters 2) perform a sanitary check for the parameters presented by the user

I know about each controller interceptor how I can write a global common interceptor.

how and where can i call this function in grails? Thanks in advance.

+4
source share
3 answers

You can use beforeInterceptor in the controller for exactly the same purpose. Put this in a new controller, say BaseController, and put everything you want to do, and then distribute all existing controllers with BaseController, and you're ready to go. For more information about beforeInterceptor Click here.

Note If you already have a common controller that is expandable by all other controllers, there is no need to implement a new controller. Just implement beforeInterceptor in this same controller.

In addition, you can use the filter and do everything that is in this filter. For more information about filters in Grails, click here.

+6
source

You can add a filter as follows to intercept every action

class AbcFilters { def filters = { abc1(controller: '*', action: '*') { } abc2(controller: '*Test', action: '*') { } abc2(controllerExclude: 'AppStartup*', action: '*') { } } } 
+4
source

Here is a filter for each action in each controller:

 all(controller: '*', action: '*') { before = { } after = { } afterView = { } } 

here is a filter to verify the session that spring security uses:

  auditEntry(controller:'*', action:'*') { before = { if (session.isNew()){ def userName def principal = SCH?.context?.authentication?.principal if (principal instanceof String){ userName = principal log.info "Anonymous user has logged into the application." }else{ userName = principal?.username log.info "User $userName has logged into the application." log.debug (principal) } } } } 
+3
source

Source: https://habr.com/ru/post/1403572/


All Articles