增加session验证

This commit is contained in:
张超
2025-06-13 08:59:01 +08:00
parent 7913a2b381
commit 67c85327ee
8 changed files with 201 additions and 207 deletions

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"go_blog/controllers"
"go_blog/models"
"go_blog/pkg/jwt"
"go_blog/serializers"
"go_blog/themes" // <-- 确保导入 themes 包
"log"
@@ -12,31 +11,53 @@ import (
"strconv"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// 新增:自定义后台认证中间件(处理页面重定向)
// 自定义后台认证中间件通过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 != ""
// 已登录状态访问登录/注册页:重定向到后台首页
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
}
c.Next()
}
}
// 新增基于Session的认证中间件
func SessionAuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
currentPath := c.Request.URL.Path
user, _ := c.Get("user") // 从jwt中间件获取用户信息
// 已登录状态访问登录/注册页:重定向到后台首页
if (currentPath == "/admin/login" || currentPath == "/admin/register") && user != nil {
c.Redirect(http.StatusFound, "/admin/index")
c.Abort()
return
}
// 未登录状态访问非登录/注册页:重定向到登录页
if (currentPath != "/admin/login" && currentPath != "/admin/register") && user == nil {
session := sessions.Default(c)
userID := session.Get("user_id")
if userID == nil {
// 未登录,重定向到登录页
c.Redirect(http.StatusFound, "/admin/login")
c.Abort()
return
}
// 可选根据userID查询用户信息并注入上下文
// user, err := models.GetAccountByID(userID.(uint))
// if err != nil { ... }
// c.Set("user", user)
c.Next()
}
}
@@ -137,23 +158,30 @@ func RegisterRoutes(r *gin.Engine) {
r.GET("/post/:id", controllers.ShowPost)
admin := r.Group("/admin")
admin.Use(CheckAdminAuth()) // 使用自定义重定向中间件
admin.Use(CheckAdminAuth())
{
// 无需认证的公开路由(登录/注册)
// 新增:处理 /admin 根路径跳转
admin.GET("/", func(c *gin.Context) {
// 由于 CheckAdminAuth 中间件已处理未登录场景,此处用户必定已登录
c.Redirect(http.StatusFound, "/admin/index")
})
admin.GET("/login", controllers.ShowLoginPage)
admin.GET("/register", controllers.ShowRegisterPage)
admin.POST("/login", controllers.UsersLoginHandler)
admin.POST("/register", controllers.UsersRegisterHandler)
// 需要认证的路由组使用jwt中间件验证令牌
authAdmin := admin.Group("", jwt.AuthRequired())
// 需要认证的路由组
authAdmin := admin.Group("", SessionAuthRequired())
{
authAdmin.GET("/index", controllers.ShowAdminIndexPage) // 后台首页
authAdmin.GET("/themes", controllers.ListThemes) // 主题列表
authAdmin.GET("/themes/switch", controllers.ShowThemeSwitchPage) // 新增:主题切换页面
authAdmin.POST("/themes/switch", controllers.SwitchTheme) // 原有:处理主题切换提交
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)
admin.GET("/logout", controllers.UsersLogoutHandler) // 添加退出路由
}
}