You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/middleware/session.go

31 lines
788 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package middleware
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"net/http"
)
// SessionHandler 创建并返回会话中间件
func SessionHandler() gin.HandlerFunc {
store := SessionConfig()
return sessions.Sessions("GOFLY", store)
}
// SessionConfig 配置会话存储
func SessionConfig() sessions.Store {
sessionMaxAge := 3600
sessionSecret := "GOFLY"
store := cookie.NewStore([]byte(sessionSecret))
store.Options(sessions.Options{
MaxAge: sessionMaxAge, // seconds
Path: "/",
HttpOnly: true, // 建议添加防止XSS攻击
Secure: true, // 生产环境建议启用要求HTTPS
SameSite: http.SameSiteLaxMode, // 防止CSRF攻击
})
return store
}