session.go 439 B

123456789101112131415
  1. package middleware
  2. import (
  3. "github.com/gin-contrib/sessions"
  4. "github.com/gin-contrib/sessions/cookie"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // Session 初始化session
  8. func Session(secret string) gin.HandlerFunc {
  9. store := cookie.NewStore([]byte(secret))
  10. //Also set Secure: true if using SSL, you should though
  11. store.Options(sessions.Options{HttpOnly: true, MaxAge: 7 * 86400, Path: "/"})
  12. return sessions.Sessions("gin-session", store)
  13. }