2

我正在试验 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
    }
}
  1. matchAll().includes(...)实际上并不存在于Matcher对象上。那么我该怎么说“只拦截对”的请求AccountController呢?
  2. 如果您遵循身份验证逻辑,如果用户当前anonymous(未登录),我想将他们重定向到AuthController#signin操作,这将为他们提供登录屏幕。拦截器似乎无法使用redirect(...)闭包……那么如何安全地执行此重定向?此外,我如何“保存”我们当前拦截的 URL,以便在成功登录后,用户可以再次被重定向到最初请求的 URL?

我在上面安全地CannotRedirectException说,因为如果太多重定向不断被抛出,我会遇到 Grails 被抛出的问题,并且这些错误通常通过在按照之前的答案执行重定向后返回来缓解。

4

1 回答 1

1

对于#1, match(controller: "account" ) 应该可以解决问题。不知道#2的答案。

于 2015-05-06T12:07:11.503 回答