簡體   English   中英

Grails 3攔截器和控制器示例

[英]Grails 3 interceptors and controller by example

我正在嘗試Grails 3及其攔截器的新概念。 給定以下攔截器/控制器:

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. AuthServiceGrails服務 我想那里是一個實例AuthService每個實例AuthController 而且我希望AuthController具有prototype范圍,這樣我就永遠不會存儲狀態,並且會為每個請求創建一個控制器。 為了滿足這些范圍要求,我必須同時更改兩個( AuthService / AuthController )?
  2. 假設AuthController#signin是由於攔截器內的redirect(controller:auth, action: signin)執行的,我該如何(再次)將用戶重定向到其預期的目標(即,他們想要在訪問之前到達的URL)從上面的控制器動作內部攔截器攔截請求?

首先,如果您重定向到另一個URL,則必須返回false以取消呈現過程,例如

 redirect(controller: auth, action: signin)
 false

其次,如果要返回預覽預期的URL,則必須將其保存到會話中,然后在完成登錄過程后重定向到已保存的URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM