gorilla/sessions

Package gorilla/sessions provides cookie and filesystem sessions and infrastructure for custom session backends.

Github星跟蹤圖

sessions

GoDoc Build Status
Sourcegraph

gorilla/sessions provides cookie and filesystem sessions and infrastructure for
custom session backends.

The key features are:

  • Simple API: use it as an easy way to set signed (and optionally
    encrypted) cookies.
  • Built-in backends to store sessions in cookies or the filesystem.
  • Flash messages: session values that last until read.
  • Convenient way to switch session persistency (aka "remember me") and set
    other attributes.
  • Mechanism to rotate authentication and encryption keys.
  • Multiple sessions per request, even using different backends.
  • Interfaces and infrastructure for custom session backends: sessions from
    different stores can be retrieved and batch-saved using a common API.

Let's start with an example that shows the sessions API in a nutshell:

	import (
		"net/http"
		"github.com/gorilla/sessions"
	)

	// Note: Don't store your key in your source code. Pass it via an
	// environmental variable, or flag (or both), and don't accidentally commit it
	// alongside your code. Ensure your key is sufficiently random - i.e. use Go's
	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
	var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))

	func MyHandler(w http.ResponseWriter, r *http.Request) {
		// Get a session. We're ignoring the error resulted from decoding an
		// existing session: Get() always returns a session, even if empty.
		session, _ := store.Get(r, "session-name")
		// Set some session values.
		session.Values["foo"] = "bar"
		session.Values[42] = 43
		// Save it before we write to the response/return from the handler.
		err = session.Save(r, w)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

First we initialize a session store calling NewCookieStore() and passing a
secret key used to authenticate the session. Inside the handler, we call
store.Get() to retrieve an existing session or create a new one. Then we set
some session values in session.Values, which is a map[interface{}]interface{}.
And finally we call session.Save() to save the session in the response.

More examples are available on the Gorilla
website
.

Store Implementations

Other implementations of the sessions.Store interface:

License

BSD licensed. See the LICENSE file for details.

主要指標

概覽
名稱與所有者gorilla/sessions
主編程語言Go
編程語言Go (語言數: 2)
平台
許可證BSD 3-Clause "New" or "Revised" License
所有者活动
創建於2012-10-02 21:33:02
推送於2024-08-20 14:12:54
最后一次提交
發布數9
最新版本名稱v1.4.0 (發布於 )
第一版名稱v1.1 (發布於 )
用户参与
星數3.1k
關注者數55
派生數372
提交數152
已啟用問題?
問題數166
打開的問題數3
拉請求數85
打開的拉請求數4
關閉的拉請求數30
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?