session

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

* @Author: reel * @Date: 2023-06-06 22:40:43 * @LastEditors: reel * @LastEditTime: 2024-03-26 07:01:09 * @Description: session初始化相关设置

* @Author: reel * @Date: 2023-06-06 19:21:05 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:45:16 * @Description: session 模块

Index

Constants

This section is empty.

Variables

View Source
var (
	ERROR_SESSION_LENGTH     = errorx.New("无法正确获取到session")
	ERROR_SESSION_NOT_LOGIN  = errorx.New("账户未登陆或登陆信息已生效,请重新登陆")
	ERROR_SESSION_ELSE_LOGIN = errorx.New("已在其他地方登陆, 请重新登陆")
)

Functions

func Cookiename

func Cookiename(cookieName string) optFunc

设置客户端存储的cookie名称 默认名称sid

func GenSessionKey

func GenSessionKey() string

func Lifetime

func Lifetime(lifeTime int) optFunc

设置过期时间 默认1800秒, 30分钟

func Prefix

func Prefix(prefix string) optFunc

func Singular added in v1.1.0

func Singular(singular string) optFunc

设置是否单用户登陆

func Store

func Store(cache cache.Store) optFunc

设置存储, 默认使用本地缓存, 底层为buntdb作为缓存支撑, 接口为store/cache.store

Types

type Session

type Session interface {
	CookieName() string
	Singular() string // 是否单用户登陆, Y表示是, N或空表示否
	SetWithToken(sessionKey, sessionValue string)
	GetWithCookie(r *http.Request) (cookieValue, value string, err error)
	SetWithCookie(w http.ResponseWriter, cookieValue, internalValue string)
	GetWithToken(r *http.Request) (sessionKey, sessionValue string, err error)
	GetSessionWithCookie(r *http.Request, w http.ResponseWriter) (sessionValue string, err error)
	SetWithSid(w http.ResponseWriter, cookieValue, internalValue string)
	GetWithSid(r *http.Request) (sessionKey, sessionValue string, err error)

	// 服务端想客户端设置cookie, 使用请求头的X-CSRF-TOKEN字段
	SetWithCsrfToken(w http.ResponseWriter, cookieValue, internalValue string)

	// 客户端发送请求, 使用cookie传输
	GetWithCsrfToken(r *http.Request) (sessionKey, sessionValue string, err error)
	// contains filtered or unexported methods
}

func New

func New(funs ...optFunc) Session

默认有效期30分钟 默认cookiename=sid, 单用户登陆 默认使用本地缓存, 底层为buntdb作为缓存支撑, 接口为store/cache.store

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL