Go?WEB框架使用攔截器驗證用戶登錄狀態(tài)實現(xiàn)
wego攔截器
wego攔截器是一個action(處理器函數(shù))之前或之后被調用的函數(shù),通常用于處理一些公共邏輯。攔截器能夠用于以下常見問題:
- 請求日志記錄
- 錯誤處理
- 身份驗證處理
wego中有以下攔截器:
- before_exec :執(zhí)行action之前攔截器
- after_exec :執(zhí)行action之后攔截器
本文用一個例子來說明如何使用攔截器來實現(xiàn)用戶登錄狀態(tài)的判定。在這個例子中,用戶訪問login_get來顯示登錄頁面,并調用login_post頁面來提交登錄信息。
在login_post頁面中判定用戶登錄信息是否合法,并將登錄賬號保存在session中。用戶訪問其他需要登錄驗證的頁面(例如:index頁面)前首先執(zhí)行攔截器:handler.BeforeExec。
在攔截器中獲取用戶賬號,若沒有獲取到用戶賬號,則跳轉到登錄頁面:login_get。使用攔截器來進行用戶登錄狀態(tài)的檢查的有點是,不用在每個處理器函數(shù)中都包含用戶登錄狀態(tài)的檢查邏輯。只要將登錄邏輯獨立出來,并實現(xiàn)為before_exec攔截器即可。
以下時main函數(shù)的主要內容:
main函數(shù)
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數(shù)據(jù)存儲引擎,用于存儲用戶登錄賬號。
- 調用
web.BeforExec(handler.BeforeExec)來設置攔截器,在handler.BeforeExec攔截器中實現(xiàn)了登錄狀態(tài)的檢查邏輯。 - 由于login_get頁面、login_post頁面不需要進行登錄檢驗,使用
SkipHook()來忽略攔截器。
登錄邏輯
用戶訪問需要進行登錄驗證的頁面時,首先會檢查session的登錄賬號,若沒有登錄賬號,則跳轉到登錄頁面:login_get, 登錄頁面的處理器實現(xiàn)如下:
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>用戶點擊"立即登錄"后項服務器發(fā)送post請求到login_post, login_post處理器的實現(xiàn)如下:
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。
登錄攔截器的實現(xiàn)
登錄檢查的邏輯采用before_exec攔截器來實現(xiàn),以下是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
}
}說明:
- 實現(xiàn)GetAccount函數(shù)來獲取session數(shù)據(jù),若用戶成功登錄了系統(tǒng),則session中保存有用戶的登錄賬號。在處理器函數(shù)中可以調用GetAccount函數(shù)來獲取登錄的登錄賬號。
- BeforeExec函數(shù)是before_exec攔截器的一個實現(xiàn)。在BeforeExec函數(shù)中首先調用GetAccount函數(shù)來獲取登錄的登錄賬號,若不存在用戶賬號,則跳轉到登錄頁面:login_get,否則執(zhí)行處理去函數(shù)。
index頁面的實現(xiàn)
index頁面需要驗證用戶是否登錄,由于執(zhí)行index頁面的處理器前會執(zhí)行before_exec攔截器, 因此在index頁面的處理器函數(shù)中不需要再進行登錄檢查了,程序員只需要實現(xiàn)業(yè)務邏輯即可。index頁面的處理器函數(shù)的實現(xiàn)代碼如下:
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
以上就是Go WEB框架使用攔截器驗證用戶登錄狀態(tài)實現(xiàn)的詳細內容,更多關于Go WEB驗證用戶登錄狀態(tài)的資料請關注腳本之家其它相關文章!
相關文章
Go語言中init函數(shù)與匿名函數(shù)使用淺析
這篇文章主要介紹了Go語言中init函數(shù)與匿名函數(shù)使用淺析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
go語言使用Chromedp實現(xiàn)二維碼登陸教程示例源碼
這篇文章主要為大家介紹了go語言使用Chromedp實現(xiàn)二維碼登陸示例源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04
golang數(shù)組和切片作為參數(shù)和返回值的實現(xiàn)
本文主要介紹了golang數(shù)組和切片作為參數(shù)和返回值的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

