簡體   English   中英

如何在不使用帶有grails 3的Spring安全性的情況下使用攔截器創建登錄功能

[英]How to create a login function without using spring security with grails 3 using interceptors

系統有兩個grails應用程序:

  1. 使用springsecurity的私有后台和一個包含運營商用戶名,密碼,登錄失敗次數等的運營商域對象。
  2. 用戶注冊,登錄和使用系統的公共Web前端。 用戶域對象包含用戶的用戶名,密碼等。

因為我們使用springsecuirty作為后台,我假設我們不能再次使用它來進行Web(配置和數據庫會發生沖突)。 此外,Web只需要一個非常基本的auth(所有頁面都需要一個有效的會話,除了注冊表和登錄表單本身)。

設置登錄表單和攔截器很容易。

問題是,登錄表單應該在控制器中實際執行什么操作? 我可以檢查用戶名和密碼匹配數據庫中的什么,然后我可能需要創建一個會話,會話超時等。我在哪里尋找有關如何執行此操作的文檔? http://docs.grails.org/3.1.1/ref/Servlet%20API/session.html告訴您如何注銷,但不能登錄。 我可能需要在DB中存儲會話(以便用戶可以訪問任何服務器)等。

通過查看我的一些舊的Java代碼,我有一些方法。

攔截器看起來像這樣:

class AuthInterceptor {
    public AuthInterceptor() {
        // allow the login form and register form to work.
        matchAll().excludes(controller: 'auth')
    }
    boolean before() {
        if(session.getAttribute("user")== null ) {
            // jump to the login form if there is no user attribute.
            redirect controller: 'auth', action: 'login'
            return false
        }
        true
    }
    boolean after() { true }
    void afterView() {
        // no-op
    }

控制器看起來像這樣:

class AuthController {
    def index() { }

    def login() {
        def email = params.email
        def password = params.password

        if (email != null) {
            // It would be better to invalidate the old session
            // but if we do, we cant get a new one...
            // session.invalidate()

            User user = User.findByEmail(email);
            if (user != null) {
                log.error("user.pass:" +  user.password +  " pass:" + password)
                // @TODO handle password encryption
                if (user.password == password) {
                    session.setAttribute("user", user)
                    redirect(controller:"dashboard")
                }
            }
            flash.message = "email or password incorrect"
        }
        render (view:"login")
    } // login()

但是,我還沒有找到我們可以設置會話超時的位置。

暫無
暫無

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

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