golang gf框架
代码:
package main
import (
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/glog"
)
const (
usernameIN = "admin"
passwdIN = "admin"
sessionKey = "adminLogin"
)
func AuthMiddleware(re *ghttp.Request) {
if re.Session.Get(sessionKey) != nil {
glog.Info("已经登陆成功")
re.Middleware.Next()
} else {
user := re.FormValue("user")
passwd := re.FormValue("passwd")
glog.Info("进入鉴权中间件", user, passwd)
if user == usernameIN && passwd == passwdIN {
glog.Info("登陆成功!")
re.Session.Set(sessionKey, true)
re.Middleware.Next()
} else {
var loginHtml string = `
Title提交
`
re.Response.Write(loginHtml)
glog.Info("登陆失败重新登陆!")
return
}
}
}
顺便吐糟一下,java里这个东东叫拦截器,golang里叫中间件,(⊙o⊙)…