sessionKit

package
v3.0.14 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSession

func GetSession(req *http.Request, store sessions.Store, cookieName string) (*sessions.Session, error)

GetSession 尝试获取一个 *sessions.Session 实例(优先用已有的,没的话再创建).

Description: 直接使用 源 即可.

PS: 可以通过 sessions.Session 的 IsNew 属性判断该实例是"新创建的"还是"原有的".

@param req e.g. gin中的ctx.Request @param cookieName 浏览器端cookie的name

func NewCookieStore

func NewCookieStore(keyPairs []byte, options *sessions.Options) *sessions.CookieStore

NewCookieStore

PS: 以 sessions.NewCookieStore() 为基础进行修改.

@param keyPairs []byte("0123456789abcdef0123456789abcdef") @param options 可以为nil(将采用默认值)

func SaveSession

func SaveSession(req *http.Request, writer http.ResponseWriter, session *sessions.Session) error

SaveSession

Description: 直接使用 源 即可.

@param req e.g. gin中的ctx.Request @param writer e.g. gin中的ctx.Writer

Types

type GobSerializer

type GobSerializer struct{}

func (GobSerializer) Deserialize

func (gs GobSerializer) Deserialize(d []byte, s *sessions.Session) error

func (GobSerializer) Serialize

func (gs GobSerializer) Serialize(s *sessions.Session) ([]byte, error)

type KeyGenFunc

type KeyGenFunc func() (string, error)

KeyGenFunc defines a function used by store to generate a key

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore stores gorilla sessions in Redis

func NewRedisStore

func NewRedisStore(ctx context.Context, client redis.UniversalClient, redisKeyPrefix string, keyGen KeyGenFunc,
	opts sessions.Options, expirationForZeroMaxAge time.Duration) (*RedisStore, error)

NewRedisStore returns a new RedisStore with default configuration

PS: (1) cookie的value 由 传参keyGen 生成(即Redis中session键的后半部分).

e.g.

同一个Session,cookie的值: "01H815Q2YFYEFDZHAQ2478FFS9", Redis中session键: "session:01H815Q2YFYEFDZHAQ2478FFS9".

@param redisKeyPrefix Redis中session键的前半部分(固定) @param keyGen Redis中session键的后半部分(不固定,动态生成)|| cookie的value

(a) 可以为nil,将采用默认值

@param opts Cookie的属性 @param expirationForZeroMaxAge (a) sessions.Options.MaxAge == 0的情况下,此属性才有效

(b) 传值可以参考 redisKit.Client 的 Set().

func (*RedisStore) Close

func (s *RedisStore) Close() error

Close closes the Redis store

func (*RedisStore) Get

func (s *RedisStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a session for the given name after adding it to the registry.

func (*RedisStore) KeyGen

func (s *RedisStore) KeyGen(f KeyGenFunc)

KeyGen sets the key generator function

func (*RedisStore) KeyPrefix

func (s *RedisStore) KeyPrefix(keyPrefix string)

KeyPrefix sets the key prefix to store session in Redis

func (*RedisStore) New

func (s *RedisStore) New(r *http.Request, name string) (*sessions.Session, error)

New returns a session for the given name without adding it to the registry.

func (*RedisStore) Options

func (s *RedisStore) Options(opts sessions.Options)

Options set options to use when a new session is created

func (*RedisStore) Save

func (s *RedisStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response.

If the Options.MaxAge of the session is <= 0 then the session file will be deleted from the store. With this process it enforces the properly session cookie handling so no need to trust in the cookie management in the web browser.

func (*RedisStore) Serializer

func (s *RedisStore) Serializer(ss SessionSerializer)

Serializer sets the session serializer to store session

type SessionSerializer

type SessionSerializer interface {
	Serialize(s *sessions.Session) ([]byte, error)
	Deserialize(b []byte, s *sessions.Session) error
}

SessionSerializer provides an interface for serialize/deserialize a session

Jump to

Keyboard shortcuts

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