Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails 3.0.x Interceptor matchAll().excludes for multiple controllers

Following Grails 3.0.11 Interceptors document, I code my own Interceptors as below:

class AuthInterceptor {
    int order = HIGHEST_PRECEDENCE;
    AuthInterceptor() {
        println("AuthInterceptor.AuthInterceptor(): Enter..............");
        // ApiController.index() and HomeController.index() don't need authentication.
        // Other controllers need to check authentication

        matchAll().excludes {
            match(controller:'api', action:'index);
            match(controller:'home', action:'index');
        }
    }
    boolean before() {
        println "AuthInterceptor.before():Enter----------------->>>>>>";
        log.debug("AuthInterceptor.before(): params:${params}");
        log.debug("AuthInterceptor.before(): session.id:${session.id}");
        log.debug("AuthInterceptor.before(): session.user:${session.user?.englishDisplayName}");
        if (!session.user) {
            log.debug("AuthInterceptor.before(): display warning msg");
            render "Hi, I am gonna check authentication"
            return false;
        } else {
            return true;
        }
    }

    boolean after() {
        log.debug("AuthInterceptor.after(): Enter ...........");
        true
    }

    void afterView() {
        // no-op
    }
}

class P2mController {
    def index() {
        log.debug("p2m():Enter p2m()..............")
        render "Hi, I am P2M";
    }
}

When I test http://localhost:8080/p2m/index, from log console, I saw that P2mController.index() is executed without been checked authentication.

However, when I test http://localhost:8080/api/index or http://localhost:8080/home/index, AuthInterceptor.check() is executed and the browser displays

Hi, I am gonna check authentication

I wish P2mController been checked authentication, and HomeController.index() and ApiController.index() don't need to be checked authentication. But from the log and response, the result is opposite.

Where is wrong in my AuthInterceptor ?

like image 288
wureka Avatar asked Oct 31 '22 11:10

wureka


1 Answers

You want to do this instead:

matchAll().excludes(controller:'api', action:'index')
          .excludes(controller:'home', action:'index')

And don't forget the single-quote after the first 'index'.

like image 152
HypeMK Avatar answered Nov 15 '22 11:11

HypeMK