简体   繁体   中英

Grails 3 interceptors and controller by example

I am experimenting with Grails 3 and its new concept of interceptors . 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 . I would like there to be one instance of AuthService per instance of 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. What do I have to change in both ( AuthService / AuthController ) to meet these scope requirements?
  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?

First, if you redirect to another URL, you must return false to cancel the rendering process, eg

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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