2024-07-12 20:32:33 +08:00
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2024-07-13 20:33:20 +08:00
|
|
|
|
"picgo/configs"
|
|
|
|
|
"picgo/corelib"
|
|
|
|
|
"picgo/data"
|
|
|
|
|
"strings"
|
2024-07-12 20:32:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// LoginMiddleware 登录 // 添加日志中间件到路由器 使用:r.Handle("/", LoginMiddleware(http.HandlerFunc(handler)))
|
|
|
|
|
func LoginMiddleware(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-07-13 20:33:20 +08:00
|
|
|
|
resPath := r.URL.Path
|
|
|
|
|
if resPath == "/login" || resPath == "/captcha" || strings.HasPrefix(resPath, "/static") {
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
}
|
|
|
|
|
session, _ := corelib.SessionStore.Get(r, configs.Settings.Server.SessionName)
|
|
|
|
|
username, ok := session.Values["username"].(string)
|
|
|
|
|
if !ok || username == "" {
|
|
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
//user model.SysUser
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
if _, err = data.SysUserSelectByUsername(username); err != nil {
|
|
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-12 20:32:33 +08:00
|
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|