Cookie 中的secure和httponly属性

Golang中 net/http 的Cookie结构体:

// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
// HTTP response or the Cookie header of an HTTP request.
//
// See https://tools.ietf.org/html/rfc6265 for details.
type Cookie struct {
	Name  string
	Value string

	Path       string    // optional
	Domain     string    // optional
	Expires    time.Time // optional
	RawExpires string    // for reading cookies only

	// MaxAge=0 means no 'Max-Age' attribute specified.
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
	// MaxAge>0 means Max-Age attribute present and given in seconds
	MaxAge   int
	Secure   bool
	HttpOnly bool
	SameSite SameSite
	Raw      string
	Unparsed []string // Raw text of unparsed attribute-value pairs
}

其中有 SecureHttpOnly 两个属性,我们打开结构体上附加的注释,可以查阅到:

  • Secure 的作用是设置为True时,只有走HTTPS时才会带上此Cookie,如果是HTTP,则不会带上。
  • HttpOnly 的作用是设置为True时,只有和服务器的HTTP请求会带上此Cookie,如果是AJAX请求,则不会带上。


更多文章
  • 把网站去掉CSS之后
  • 处理并发的方式
  • 常见的索引方式
  • Golang 实践经验
  • 高性能MySQL笔记第一章
  • 面试的一些技巧
  • HTTP/2 简介
  • 独立运营博客一年的一些数据分享
  • To B(usiness) 和 To C(ustomer)
  • Cookie 中的secure和httponly属性
  • 常见的软件架构套路
  • Google Ads使用体验
  • Go的custom import path
  • 如何挖掘二级子域名?
  • Go Module 简明教程