Grails beforeinterceptor interceptor

Source: Internet
Author: User
Tags grails

OptionalbeforeInterceptorAttribute can interrupt the operation of an action before it starts to run.

 

Simple tracking Interceptor:

def beforeInterceptor = {       println "Tracing action ${actionUri}"}

A security verification interceptor ensures that the user has logged on:

 

def beforeInterceptor = [action:this.&auth,except:'login']// defined as a regular method so its privatedef auth() {     if(!session.user) {            redirect(action:'login')            return false     }}def login = {     // display login page}

beforeInterceptorThe interceptor can interrupt its operation before the action starts. If the interceptor returnsfalseThe action will not be executed. You can add the Interceptor to all actions in the Controller as follows:

 

def beforeInterceptor = {       println "Tracing action ${actionUri}"}

The preceding statement needs to be placed in the Controller definition. It will be executed before all actions are run, but will not affect the action processing process. A common usage is authentication:

 

def beforeInterceptor = [action:this.&auth,except:'login']// defined as a regular method so its privatedef auth() {     if(!session.user) {            redirect(action:'login')            return false     }}def login = {     // display login page}

The above Code sets a method called 'auth ', a method that will not be exposed to Action (that is, it is private ). The 'beforeinterceptor' attribute defines an interceptor for all actions (except the login action). The interceptor will execute the 'auth' method with the groovy method pointer syntax. In this method, first determine whether the current user has set a session. If not, it will redirect to the login action and return false, other code of the interrupted action will not be executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.