# Cookie 处理

这部分的例子在Cookie example (opens new window)

Beego 通过Context直接封装了对普通 Cookie 的处理方法,可以直接使用:

  • GetCookie(key string)
  • SetCookie(name string, value string, others ...interface{})

例子:

type MainController struct {
	web.Controller
}

func (ctrl *MainController) PutCookie() {
	// put something into cookie,set Expires time
	ctrl.Ctx.SetCookie("name", "web cookie", 10)

	// web-example/views/hello_world.html
	ctrl.TplName = "hello_world.html"
	ctrl.Data["name"] = "PutCookie"
	_ = ctrl.Render()
}

func (ctrl *MainController) ReadCookie() {
	// web-example/views/hello_world.html
	ctrl.TplName = "hello_world.html"
	ctrl.Data["name"] = ctrl.Ctx.GetCookie("name")
	// don't forget this
	_ = ctrl.Render()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

others参数含义依次是:

  • 第一个代表 maxAge,Beego 使用这个值计算ExpiresMax-Age两个值
  • 第二个代表Path,字符串类型,默认值是/
  • 第三个代表Domain,字符串类型
  • 第四个代表Secure,布尔类型
  • 第五个代表HttpOnly,布尔类型
  • 第六个代表SameSite,字符串类型

Beego 提供了两个方法用于辅助 Cookie 加密处理,它采用了sha256来作为加密算法,下面Secret则是加密的密钥:

  • GetSecureCookie(Secret, key string) (string, bool):用于从 Cookie 中读取数据
  • SetSecureCookie(Secret, name, value string, others ...interface{}):用于写入数据到 Cookie。
type MainController struct {
	web.Controller
}

func (ctrl *MainController) PutSecureCookie() {
	// put something into cookie,set Expires time
	ctrl.Ctx.SetSecureCookie("my-secret", "name", "web cookie")

	// web-example/views/hello_world.html
	ctrl.TplName = "hello_world.html"
	ctrl.Data["name"] = "PutCookie"
	_ = ctrl.Render()
}

func (ctrl *MainController) ReadSecureCookie() {
	// web-example/views/hello_world.html
	ctrl.TplName = "hello_world.html"
	ctrl.Data["name"], _ = ctrl.Ctx.GetSecureCookie("my-secret", "name")
	// don't forget this
	_ = ctrl.Render()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

others参数和普通 Cookie 一样。