简体   繁体   English

Grails 3拦截器和控制器示例

[英]Grails 3 interceptors and controller by example

I am experimenting with Grails 3 and its new concept of interceptors . 我正在尝试Grails 3及其拦截器的新概念。 Given the following interceptor/controller: 给定以下拦截器/控制器:

class AuthInterceptor {
    // ...other stuff in here

    boolean before() {
        if(FizzBuzz.isFoo()) {
            redirect(controller: auth, action: signin)
            true
        } else {
            true
        }
    }
}

class AuthController {
    AuthService authService

    def signin() {
        String username = params[username]
        String password = params[password]
        user = authService.authenticate(username, password)

        if(user) {
            SimpleSecurityUtils.setCurrentUser(user)
            redirect(url: ??? INTENDED_DESTINATION ???)
        } else {
            // Auth failed.
            redirect(action: failed)
        }
    }
}
  1. AuthService is a Grails Service . AuthServiceGrails服务 I would like there to be one instance of AuthService per instance of AuthController . 我想那里是一个实例AuthService每个实例AuthController And I would like AuthController to have prototype scope such that I am never storing state, and a controller is created for each request. 而且我希望AuthController具有prototype范围,这样我就永远不会存储状态,并且会为每个请求创建一个控制器。 What do I have to change in both ( AuthService / AuthController ) to meet these scope requirements? 为了满足这些范围要求,我必须同时更改两个( AuthService / AuthController )?
  2. Assuming AuthController#signin is executed due to a redirect(controller:auth, action: signin) inside an interceptor, how do I redirect (yet again) the user to their intended destination (that is, the URL they wanted to go to prior to the interceptor intercepting the request) from inside the controller action above? 假设AuthController#signin是由于拦截器内的redirect(controller:auth, action: signin)执行的,我该如何(再次)将用户重定向到其预期的目标(即,他们想要在访问之前到达的URL)从上面的控制器动作内部拦截器拦截请求?

First, if you redirect to another URL, you must return false to cancel the rendering process, eg 首先,如果您重定向到另一个URL,则必须返回false以取消呈现过程,例如

 redirect(controller: auth, action: signin)
 false

Second, if you want back to previews intended URL, you must save it, may be, to session, and then you redirect to the saved URL when you finish signin process. 其次,如果要返回预览预期的URL,则必须将其保存到会话中,然后在完成登录过程后重定向到已保存的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM