6

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 ?

wureka
  • 731
  • 1
  • 11
  • 26

1 Answers1

4

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'.

HypeMK
  • 331
  • 3
  • 9
  • 1
    #HyperMK, thanks for your suggestion. Well, I do know this way. But I wonder if I can provide a list such as [{controller:'api', action:'index'}, {controller:'home'}] to the closure of matchAll().excludes() {.....} – wureka Jan 28 '16 at 02:41