日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Go?WEB框架使用攔截器驗證用戶登錄狀態實現_Golang

作者:haming ? 更新時間: 2022-09-16 編程語言

wego攔截器

wego攔截器是一個action(處理器函數)之前或之后被調用的函數,通常用于處理一些公共邏輯。攔截器能夠用于以下常見問題:

  • 請求日志記錄
  • 錯誤處理
  • 身份驗證處理

wego中有以下攔截器:

  • before_exec :執行action之前攔截器
  • after_exec :執行action之后攔截器

本文用一個例子來說明如何使用攔截器來實現用戶登錄狀態的判定。在這個例子中,用戶訪問login_get來顯示登錄頁面,并調用login_post頁面來提交登錄信息。

在login_post頁面中判定用戶登錄信息是否合法,并將登錄賬號保存在session中。用戶訪問其他需要登錄驗證的頁面(例如:index頁面)前首先執行攔截器:handler.BeforeExec。

在攔截器中獲取用戶賬號,若沒有獲取到用戶賬號,則跳轉到登錄頁面:login_get。使用攔截器來進行用戶登錄狀態的檢查的有點是,不用在每個處理器函數中都包含用戶登錄狀態的檢查邏輯。只要將登錄邏輯獨立出來,并實現為before_exec攔截器即可。

以下時main函數的主要內容:

main函數

package main
import (
    "demo/handler"
    "github.com/haming123/wego"
    log "github.com/haming123/wego/dlog"
)
func main() {
    web, err := wego.InitWeb()
    if err != nil {
        log.Error(err)
        return
    }
    wego.SetDebugLogLevel(wego.LOG_DEBUG)
    web.Config.SessionParam.SessionOn = true
    web.Config.SessionParam.HashKey = "demohash"
    web.BeforExec(handler.BeforeExec)
    web.GET("/login_get", handler.HandlerLoginGet).SkipHook()
    web.POST("/login_post", handler.HandlerLoginPost).SkipHook()
    web.GET("/index", handler.HandlerIndex)
    err = web.Run("0.0.0.0:8080")
    if err != nil {
        log.Error(err)
    }
}

說明:

  • 本例子中使用基于cookie的session數據存儲引擎,用于存儲用戶登錄賬號。
  • 調用web.BeforExec(handler.BeforeExec)來設置攔截器,在handler.BeforeExec攔截器中實現了登錄狀態的檢查邏輯。
  • 由于login_get頁面、login_post頁面不需要進行登錄檢驗,使用SkipHook()來忽略攔截器。

登錄邏輯

用戶訪問需要進行登錄驗證的頁面時,首先會檢查session的登錄賬號,若沒有登錄賬號,則跳轉到登錄頁面:login_get, 登錄頁面的處理器實現如下:

func HandlerLoginGet(c *wego.WebContext) {
    c.WriteHTML(http.StatusOK, "./view/login.html", nil)
}

login.html的內容如下:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登錄頁面</title>
</head>
<body>
<h2>用戶登陸</h2>
<form action="/login_post" method="post">
    <div>用戶賬號:</div>
    <div>
        <input type="text" name="account" placeholder="請輸入用戶賬號" />
    </div>
    <br />
    <div>登錄密碼:</div>
    <div>
        <input type="password" name="password" placeholder="請輸入登錄密碼"/>
    </div>
    <br />
    <div>
        <input type="submit" value="立即登錄" />
    </div>
</form>
</body>
</html>

用戶點擊"立即登錄"后項服務器發送post請求到login_post, login_post處理器的實現如下:

func HandlerLoginPost(c *wego.WebContext) {
    account := c.Param.MustString("account")
    password := c.Param.MustString("password")
    if account == "admin" && password == "demo" {
        c.Session.Set("account", account)
        c.Session.Save()
        c.Redirect(302, "/index")
    } else {
        c.Session.Set("account", "")
        c.Session.Save()
        c.Redirect(302, "/login_get")
    }
}

說明:

  • 在HandlerLoginPost調用c.Session.Set將賬號寫入session。

登錄攔截器的實現

登錄檢查的邏輯采用before_exec攔截器來實現,以下是before_exec攔截器的代碼:

func GetAccount(c *wego.WebContext) string {
    account, _ := c.Session.GetString("account")
    return account
}
func BeforeExec(c *wego.WebContext) {
    login_path := "/login_get"
    if GetAccount(c) == "" && c.Path != login_path {
        c.Redirect(302, login_path)
        return
    }
}

說明:

  • 實現GetAccount函數來獲取session數據,若用戶成功登錄了系統,則session中保存有用戶的登錄賬號。在處理器函數中可以調用GetAccount函數來獲取登錄的登錄賬號。
  • BeforeExec函數是before_exec攔截器的一個實現。在BeforeExec函數中首先調用GetAccount函數來獲取登錄的登錄賬號,若不存在用戶賬號,則跳轉到登錄頁面:login_get,否則執行處理去函數。

index頁面的實現

index頁面需要驗證用戶是否登錄,由于執行index頁面的處理器前會執行before_exec攔截器, 因此在index頁面的處理器函數中不需要再進行登錄檢查了,程序員只需要實現業務邏輯即可。index頁面的處理器函數的實現代碼如下:

func HandlerIndex(c *wego.WebContext) {
    c.WriteHTML(http.StatusOK, "./view/index.html", GetAccount(c))
}

在HandlerIndex中讀取index.html模板,并使用調用 GetAccount(c)獲取用戶賬號,然后及進行渲染。其中index.html模板的內容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
hello : {{.}}
</body>
</html>

wego代碼的下載

go get?https://github.com/haming123/wego

原文鏈接:https://segmentfault.com/a/1190000042177981

欄目分類
最近更新