網站首頁 編程語言 正文
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
相關推薦
- 2022-03-14 Go使用Gin框架開發博客系統(一)之環境搭建和項目初始化
- 2022-09-02 Python中的Numpy?面向數組編程常見操作_python
- 2022-11-05 Android?View轉換為Bitmap實現應用內截屏功能_Android
- 2022-02-18 IE11打開后顯示空白頁,報錯:1002||不支持XXX屬性或方法
- 2022-12-21 C#?wpf?無邊框窗口添加陰影效果的實現_C#教程
- 2023-02-02 Go并發與鎖的兩種方式該如何提效詳解_Golang
- 2022-03-07 C++中簡單的文本文件輸入/輸出示例詳解_C 語言
- 2022-10-06 Go語言實現常用排序算法的示例代碼_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支