add admin router

This commit is contained in:
张超
2025-06-18 18:34:08 +08:00
parent 67c85327ee
commit 4c9f6c140f
4 changed files with 32 additions and 86 deletions

View File

@@ -19,28 +19,28 @@ import (
// 新增:自定义后台认证中间件(处理页面重定向)
// 自定义后台认证中间件通过Session判断登录状态
func CheckAdminAuth() gin.HandlerFunc {
return func(c *gin.Context) {
currentPath := c.Request.URL.Path
session := sessions.Default(c) // 获取当前请求的Session
userID := session.Get("user_id") // 从Session中获取用户ID
loggedIn := userID != nil && userID != ""
return func(c *gin.Context) {
currentPath := c.Request.URL.Path
session := sessions.Default(c) // 获取当前请求的Session
userID := session.Get("user_id") // 从Session中获取用户ID
loggedIn := userID != nil && userID != ""
// 已登录状态访问登录/注册页:重定向到后台首页
if (currentPath == "/admin/login" || currentPath == "/admin/register") && loggedIn {
c.Redirect(http.StatusFound, "/admin/index")
c.Abort()
return
}
// 已登录状态访问登录/注册页:重定向到后台首页
if (currentPath == "/admin/login" || currentPath == "/admin/register") && loggedIn {
c.Redirect(http.StatusFound, "/admin/index")
c.Abort()
return
}
// 未登录状态访问非登录/注册页:重定向到登录页
if (currentPath != "/admin/login" && currentPath != "/admin/register") && !loggedIn {
c.Redirect(http.StatusFound, "/admin/login")
c.Abort()
return
}
// 未登录状态访问非登录/注册页:重定向到登录页
if (currentPath != "/admin/login" && currentPath != "/admin/register") && !loggedIn {
c.Redirect(http.StatusFound, "/admin/login")
c.Abort()
return
}
c.Next()
}
c.Next()
}
}
// 新增基于Session的认证中间件
@@ -177,7 +177,6 @@ func RegisterRoutes(r *gin.Engine) {
{
authAdmin.GET("/index", controllers.ShowAdminIndexPage)
authAdmin.GET("/themes", controllers.ListThemes)
authAdmin.GET("/themes/switch", controllers.ShowThemeSwitchPage)
authAdmin.POST("/themes/switch", controllers.SwitchTheme)
authAdmin.POST("/setinfo", controllers.UsersSetInfoHandler)
authAdmin.POST("/setpwd", controllers.UsersSetPwdHandler)