Gin框架中的Cookie怎么搞(会话控制)

参考地址

设置和获取 Cookie | Gin Web Framework (gin-gonic.com)icon-default.png?t=N7T8https://gin-gonic.com/zh-cn/docs/examples/cookie/

什么是cookie

cookie在互联网上随处可见,具体体现如下:

保持登录状态
保存浏览器的历史记录
大数据随心配,按喜好推送讯息
购物网站加入购物车

都会用到cookie来保存一些信息;

Cookie 介绍 

  • HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分两次请求是否由同一个客户端发出
  • HTTP 是无状态协议。简单地说,当你浏览了一个页面,然后转到同一个网站的另一个页
    面,服务器无法认识到这是同一个浏览器在访问同一个网站。每一次的访问,都是没有任何
    关系的。如果我们要实现多个页面之间共享数据的话我们就可以使用 Cookie 或者 Session
  • cookie 是存储于访问者计算机的浏览器中。可以让我们用同一个浏览器访问同一个域名
    的时候共享数据。

Cookie的用途

  • 测试服务端发送cookie给客户端,客户端请求时携带cookie

设置 Cookie 

 Set-Cookie将Set-Cookie标头添加到ResponseWriter的标头中。提供的cookie必须有一个有效的名称。无效的cookie可能会被无提示地删除。

ctx.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

第一个参数 key
第二个参数 value
第三个参数 过期时间 . 如果只想设置 Cookie 的保存路径而不想设置存活时间,可以在第三个参数中传递 nil
第四个参数 cookie 的路径
第五个参数 cookie 的路径 Domain 作用域 本地调试配置成 localhost , 正式上线配置成域名
第六个参数是 secure ,当 secure 值为 true 时, cookie HTTP 中是无效,在 HTTPS 中才有效
第七个参数 httpOnly ,是微软对 COOKIE 做的扩展。如果在 COOKIE 中设置了 “httpOnly” 属性,则通过程序(JS 脚本、 applet 等)将无法读取到 COOKIE 信息,防止 XSS 攻击产生

获取 Cookie

cookie, err := ctx.Cookie("name") 

1、实操演练(设置获取cookie)

1、控制器设置

package test

import (
	"fmt"
	"gindemo04/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
	//设置cookie
	c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
	fmt.Println(models.UnixToTime(1757814260))
	c.HTML(http.StatusOK, "default/index.html", gin.H{
		"msg": "model测试内容--转换时间戳---如下:",
		"t":   1757814260,
	})
}
func (con ModelTest) News(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	delay ,_:=c.Cookie("delay")
	c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}

func (con ModelTest) Gouwu(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	c.String(http.StatusOK, "cookie="+username)
}

2、路由配置

package routers

import (
	"gindemo04/controllers/test"

	"github.com/gin-gonic/gin"
)

func DefaultRouters(r *gin.Engine) {
	defaultRouters := r.Group("/")
	{
		defaultRouters.GET("/", test.ModelTest{}.Index)
		defaultRouters.GET("/news", test.ModelTest{}.News)
		defaultRouters.GET("/gouwu", test.ModelTest{}.Gouwu)
		
	}
}

3、 测试

注意:这里为了方便,将main.go的端口设置为了80

 1、先去setcookie

2、再去测试cookie 

 

2、删除cookie的操作

可以设置如下:

 1、控制器做如下配置

package test

import (
	"fmt"
	"gindemo04/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
	//设置cookie
	c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
	//过期cookie设置
	c.SetCookie("delay", "过期演示", 3, "/", "localhost", false, true)

	fmt.Println(models.UnixToTime(1757814260))
	c.HTML(http.StatusOK, "default/index.html", gin.H{
		"msg": "model测试内容--转换时间戳---如下:",
		"t":   1757814260,
	})
}
func (con ModelTest) News(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	delay ,_:=c.Cookie("delay")
	c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}

func (con ModelTest) Gouwu(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	c.String(http.StatusOK, "cookie="+username)
}

func (con ModelTest) Deletcookie(c *gin.Context) {
	//删除cookie
	c.SetCookie("username", "卫宫士郎", -1, "/", "localhost", false, true)
	c.String(http.StatusOK, "删除cookie完毕,查看gouwu以及news是否还存在cookie的值")
}

2、路由配置如下

package routers

import (
	"gindemo04/controllers/test"

	"github.com/gin-gonic/gin"
)

func DefaultRouters(r *gin.Engine) {
	defaultRouters := r.Group("/")
	{
		defaultRouters.GET("/", test.ModelTest{}.Index)
		defaultRouters.GET("/news", test.ModelTest{}.News)
		defaultRouters.GET("/gouwu", test.ModelTest{}.Gouwu)
		defaultRouters.GET("/deletcookie", test.ModelTest{}.Deletcookie)
	}
}

3、测试

再去查看gouwu就没有cookie了

 

 3、过期cookie的操作

设置时间短一些

package test

import (
	"fmt"
	"gindemo04/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
	//设置cookie
	c.SetCookie("username", "卫宫士郎", 3600, "/", "localhost", false, true)
	//过期cookie设置
	c.SetCookie("delay", "过期演示", 5, "/", "localhost", false, true)
	//二级域名cookie共享(联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作)
	fmt.Println(models.UnixToTime(1757814260))
	c.HTML(http.StatusOK, "default/index.html", gin.H{
		"msg": "model测试内容--转换时间戳---如下:",
		"t":   1757814260,
	})
}
func (con ModelTest) News(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	delay ,_:=c.Cookie("delay")
	c.String(http.StatusOK, "cookie=%v,delay=%v",username,delay)
}

func (con ModelTest) Gouwu(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	c.String(http.StatusOK, "cookie="+username)
}

func (con ModelTest) Deletcookie(c *gin.Context) {
	//删除cookie
	c.SetCookie("username", "卫宫士郎", -1, "/", "localhost", false, true)
	c.String(http.StatusOK, "删除cookie完毕,查看gouwu以及news是否还存在cookie的值")
}

 1、测试:

 再次刷新 ,没了

多个二级域名共享 cookie 

举个例子:

联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作

分别把 a.frank.com 和 b.frank.com 解析到我们的服务器
我们想的是用户在 a.frank.com 中设置 Cookie 信息后在 b.frank.com 中获取刚才设置的cookie,也就是实现多个二级域名共享 cookie

 在本机操作域名解析的话,需要修改C:\Windows\System32\drivers\etc下的hosts文件

最后是这个效果

 

1、 控制器配置 

package test

import (
	"fmt"
	"gindemo04/models"
	"net/http"

	"github.com/gin-gonic/gin"
)

type ModelTest struct{}
//也就是有一个控制器是设定cookie的,有去取cookies的(先取set再去取才有值)
func (con ModelTest) Index(c *gin.Context) {
	//二级域名cookie共享(联想百度账号登录百度后,再去使用百度天气可以看自己收藏的地域天气的操作)
	c.SetCookie("username","卫宫士郎",3600,"/",".frank.com",false,true)
	fmt.Println(models.UnixToTime(1757814260))
	c.HTML(http.StatusOK, "default/index.html", gin.H{
		"msg": "model测试内容--转换时间戳---如下:",
		"t":   1757814260,
	})
}
func (con ModelTest) News(c *gin.Context) {
	//获取cookie
	username,_ := c.Cookie("username")
	c.String(http.StatusOK, "cookie="+username)
}

2、路由配置

package routers

import (
	"gindemo04/controllers/test"

	"github.com/gin-gonic/gin"
)

func DefaultRouters(r *gin.Engine) {
	defaultRouters := r.Group("/")
	{
		defaultRouters.GET("/", test.ModelTest{}.Index)
		defaultRouters.GET("/news", test.ModelTest{}.News)
		
	}
}

 3、测试一下两个frank是否可以共享cookie

1、先去set cookie

2、然后去访问news(两个frank)

a.frank.com可以

b.frank.com也可以 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵唯一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值