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函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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, 登录页面的处理器实现如下:
1
2
3
|
func HandlerLoginGet(c *wego.WebContext) { c.WriteHTML(http.StatusOK, "./view/login.html" , nil ) } |
login.html的内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<!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处理器的实现如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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拦截器的代码:
1
2
3
4
5
6
7
8
9
10
11
|
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页面的处理器函数的实现代码如下:
1
2
3
|
func HandlerIndex(c *wego.WebContext) { c.WriteHTML(http.StatusOK, "./view/index.html" , GetAccount(c)) } |
在HandlerIndex中读取index.html模板,并使用调用 GetAccount(c)获取用户账号,然后及进行渲染。其中index.html模板的内容如下:
1
2
3
4
5
6
7
8
9
10
|
<!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框架使用拦截器验证用户登录状态实现的详细内容,更多关于Go WEB验证用户登录状态的资料请关注服务器之家其它相关文章!
原文链接:https://segmentfault.com/a/1190000042177981