我正在试验 Grails 3 Interceptors。给定以下拦截器:
class AuthInterceptor {
AuthInterceptor() {
matchAll().includes(controller:"account")
}
// Intercept anything under /account.
boolean before() {
User user = SimpleSecurityUtils.getCurrentUser()
if(user != SimpleSecurityUtils.anonymous) {
// Only get here if the user is currently authenticated. Redirect them to where they want to go.
true
} else {
redirect(controller: auth, action: signin)
true ??
}
}
boolean after() { true }
void afterView() {
// no-op
}
}
matchAll().includes(...)
实际上并不存在于Matcher
对象上。那么我该怎么说“只拦截对”的请求AccountController
呢?- 如果您遵循身份验证逻辑,如果用户当前
anonymous
(未登录),我想将他们重定向到AuthController#signin
操作,这将为他们提供登录屏幕。拦截器似乎无法使用redirect(...)
闭包……那么如何安全地执行此重定向?此外,我如何“保存”我们当前拦截的 URL,以便在成功登录后,用户可以再次被重定向到最初请求的 URL?
我在上面安全地CannotRedirectException
说,因为如果太多重定向不断被抛出,我会遇到 Grails 被抛出的问题,并且这些错误通常通过在按照之前的答案执行重定向后返回来缓解。