Grails 3.0.x 拦截器 matchAll().excludes 用于多个控制器

wur*_*eka 5 authentication grails interceptor grails-3.0.10

根据Grails 3.0.11 拦截器文档,我编写自己的拦截器如下:

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

当我测试http://localhost:8080/p2m/index 时,从日志控制台,我看到 P2mController.index() 在没有经过身份验证的情况下被执行。

但是,当我测试http://localhost:8080/api/indexhttp://localhost:8080/home/index 时,执行 AuthInterceptor.check() 并且浏览器显示

Hi, I am gonna check authentication
Run Code Online (Sandbox Code Playgroud)

我希望 P2mController 被检查身份验证,而 HomeController.index() 和 ApiController.index() 不需要检查身份验证。但是从日志和响应来看,结果是相反的。

我的 AuthInterceptor 哪里出了问题?

Hyp*_*eMK 5

你想这样做:

matchAll().excludes(controller:'api', action:'index')
          .excludes(controller:'home', action:'index')
Run Code Online (Sandbox Code Playgroud)

并且不要忘记第一个“索引”后面的单引号。