gpt4 book ai didi

authentication - Grails 3.0.x 拦截器 matchAll().excludes 用于多个 Controller

转载 作者:行者123 更新时间:2023-12-04 06:53:58 25 4
gpt4 key购买 nike

关注 Grails 3.0.11 Interceptors文档,我编写自己的拦截器如下:

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";
}
}

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

但是,当我测试时 http://localhost:8080/api/indexhttp://localhost:8080/home/index , AuthInterceptor.check() 被执行,浏览器显示
Hi, I am gonna check authentication

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

我的 AuthInterceptor 哪里出了问题?

最佳答案

你想这样做:

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

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

关于authentication - Grails 3.0.x 拦截器 matchAll().excludes 用于多个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34739513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com