add session check

This commit is contained in:
landaiqing
2024-10-17 16:21:46 +08:00
parent 5bfca64315
commit b5d88a7ccd
5 changed files with 70 additions and 8 deletions

View File

@@ -21,10 +21,11 @@ func InitSession(client *redis.Client) {
store.Options(sessions.Options{ store.Options(sessions.Options{
Path: "/", Path: "/",
//Domain: global.CONFIG.System.Web, //Domain: global.CONFIG.System.Web,
MaxAge: 86400 * 7, MaxAge: 86400 * 7,
HttpOnly: true, HttpOnly: true,
Secure: true, Secure: true,
SameSite: http.SameSiteLaxMode, Partitioned: true,
SameSite: http.SameSiteLaxMode,
}) })
global.Session = store global.Session = store
} }

View File

@@ -11,7 +11,7 @@ import (
"github.com/wenlng/go-captcha/v2/click" "github.com/wenlng/go-captcha/v2/click"
"github.com/wenlng/go-captcha/v2/rotate" "github.com/wenlng/go-captcha/v2/rotate"
"github.com/wenlng/go-captcha/v2/slide" "github.com/wenlng/go-captcha/v2/slide"
go_sensitive_word "github.com/zmexing/go-sensitive-word" "github.com/zmexing/go-sensitive-word"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm" "gorm.io/gorm"
"schisandra-cloud-album/config" "schisandra-cloud-album/config"

View File

@@ -0,0 +1,40 @@
package middleware
import (
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"schisandra-cloud-album/common/constant"
"schisandra-cloud-album/common/result"
"schisandra-cloud-album/utils"
)
// SessionCheckMiddleware session检查中间件
func SessionCheckMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
session := utils.GetSession(c, constant.SessionKey)
if session == nil {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
userIdAny, exists := c.Get("userId")
if !exists {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
userId, ok := userIdAny.(*string)
if !ok {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
if *userId != *session.UID {
result.FailWithCodeAndMessage(403, ginI18n.MustGetMessage(c, "AuthVerifyExpired"), c)
c.Abort()
return
}
c.Next()
}
}

View File

@@ -50,6 +50,7 @@ func InitRouter() *gin.Engine {
middleware.SecurityHeaders(), middleware.SecurityHeaders(),
middleware.JWTAuthMiddleware(), middleware.JWTAuthMiddleware(),
middleware.CasbinMiddleware(), middleware.CasbinMiddleware(),
middleware.SessionCheckMiddleware(),
middleware.VerifySignature(), middleware.VerifySignature(),
) )
{ {

View File

@@ -4,8 +4,28 @@ import (
"encoding/json" "encoding/json"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"schisandra-cloud-album/global" "schisandra-cloud-album/global"
"time"
) )
// ResponseData 返回数据
type ResponseData struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt int64 `json:"expires_at"`
UID *string `json:"uid"`
UserInfo UserInfo `json:"user_info"`
}
type UserInfo struct {
Username string `json:"username,omitempty"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Gender string `json:"gender"`
Status int64 `json:"status"`
CreateAt time.Time `json:"create_at"`
}
// SetSession sets session data with key and data // SetSession sets session data with key and data
func SetSession(c *gin.Context, key string, data interface{}) error { func SetSession(c *gin.Context, key string, data interface{}) error {
session, err := global.Session.Get(c.Request, key) session, err := global.Session.Get(c.Request, key)
@@ -28,7 +48,7 @@ func SetSession(c *gin.Context, key string, data interface{}) error {
} }
// GetSession gets session data with key // GetSession gets session data with key
func GetSession(c *gin.Context, key string) interface{} { func GetSession(c *gin.Context, key string) *ResponseData {
session, err := global.Session.Get(c.Request, key) session, err := global.Session.Get(c.Request, key)
if err != nil { if err != nil {
global.LOG.Error("GetSession failed: ", err) global.LOG.Error("GetSession failed: ", err)
@@ -39,13 +59,13 @@ func GetSession(c *gin.Context, key string) interface{} {
global.LOG.Error("GetSession failed: ", "key not found") global.LOG.Error("GetSession failed: ", "key not found")
return nil return nil
} }
var data interface{} data := ResponseData{}
err = json.Unmarshal(jsonData.([]byte), &data) err = json.Unmarshal(jsonData.([]byte), &data)
if err != nil { if err != nil {
global.LOG.Error("GetSession failed: ", err) global.LOG.Error("GetSession failed: ", err)
return nil return nil
} }
return data return &data
} }
// DelSession deletes session data with key // DelSession deletes session data with key