1. Cookie

  • HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分两次请求是否由同一个客户端发出
  • Cookie就是解决HTTP协议无状态的方案之一,中文是小甜饼的意思
  • Cookie实际上就是服务器保存在浏览器上的一段信息。浏览器有了Cookie之后,每次向服务器发送请求时都会同时将该信息发送给服务器,服务器收到请求后,就可以根据该信息处理请求
  • Cookie由服务器创建,并发送给浏览器,最终由浏览器保存

2. Cookie的用途

  • 保持用户登录状态
  • 京东购物车

3. cookie的使用

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

import (
"github.com/gin-gonic/gin"
"fmt"
) func main() {
// 创建路由
r := gin.Default()
// 服务端要给客户端cookie
r.GET("cookie", func(c *gin.Context) {
// 获取客户端是否携带cookie
cookie, err := c.Cookie("key_cookie") //c.Request.Cookie也行
if err != nil {
cookie = "NotSet"
// 给客户端设置cookie
// maxAge 单位为秒
// path cookie所在目录
// domain 域名
// secure 是否智能通过https访问
// httpOnly 是否允许别人通过js获取自己的cookie
c.SetCookie("key_cookie", "value_cookie", 60, "/",
"localhost", false, true)
}
fmt.Printf("cookie的值是: %s\n", cookie)
})
r.Run(":8000")
}

3.1 使用

package main

import (
"github.com/gin-gonic/gin"
"net/http"
) func AuthMiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
// 获取客户端cookie并校验
if cookie, err := c.Cookie("abc"); err == nil {
if cookie == "123" {
c.Next()
return
}
}
// 返回错误
c.JSON(http.StatusUnauthorized, gin.H{"error": "err"})
// 若验证不通过,不再调用后续的函数处理
c.Abort()
return
}
} func main() {
// 创建路由
r := gin.Default()
r.GET("/login", func(c *gin.Context) {
// 设置cookie
c.SetCookie("abc", "123", 60, "/",
"localhost", false, true)
// 返回信息
c.String(200, "Login success!")
})
r.GET("/home", AuthMiddleWare(), func(c *gin.Context) {
c.JSON(200, gin.H{"data": "home"})
})
r.Run(":8000")
}

3.2 Cookie的缺点

  • 不安全,明文

  • 增加带宽消耗

  • 可以被禁用

  • cookie有上限

4. Session

  • Session可以弥补Cookie的不足,Session必须依赖于Cookie才能使用,生成一个SessionId放在Cookie里传给客户端就可以

4.1 session中间件

session.go

package session

type Session interface {
Set(key string, value interface{}) error
Get(key string) (interface{}, error)
Del(key string) error
Save() error
}

session_mgr.go

package session

// 定义管理者,管理所有session
type SessionMgr interface {
// 初始化
Init(addr string, options ...string) (err error)
CreateSession() (session Session, err error)
Get(sessionId string) (session Session, err error)
}

memory.go

package session

import (
"sync"
"errors"
) // 对象
// MemorySeesion设计:
// 定义MemorySeesion对象(字段:sessionId、存kv的map,读写锁)
// 构造函数,为了获取对象
// Set()
// Get()
// Del()
// Save() type MemorySession struct {
sessionId string
// 存kv
data map[string]interface{}
rwlock sync.RWMutex
} // 构造函数
func NewMemorySession(id string) *MemorySession {
s := &MemorySession{
sessionId: id,
data: make(map[string]interface{}, 16),
}
return s
} func (m *MemorySession) Set(key string, value interface{}) (err error) {
// 加锁
m.rwlock.Lock()
defer m.rwlock.Unlock()
// 设置值
m.data[key] = value
return
} func (m *MemorySession) Get(key string) (value interface{}, err error) {
m.rwlock.Lock()
defer m.rwlock.Unlock()
value, ok := m.data[key]
if !ok {
err = errors.New("key not exists in session")
return
}
return
} func (m *MemorySession) Del(key string) (err error) {
m.rwlock.Lock()
defer m.rwlock.Unlock()
delete(m.data, key)
return
} func (m *MemorySession) Save(key string) (err error) {
return
}

memory_session_mgr.go

package session

import(
"sync" uuid "github.com/satori/go.uuid"
) // MemorySeesionMgr设计:
// 定义MemorySeesionMgr对象(字段:存放所有session的map,读写锁)
// 构造函数
// Init()
// CreateSeesion()
// GetSession() // 定义对象
type MemorySeesionMgr struct {
sessionMap map[string]Session
rwlock sync.RWMutex
} // 构造函数
func NewMemorySeesionMgr() SessionMgr {
sr := &MemorySeesionMgr{
sessionMap: make(map[string]Session, 1024),
}
return sr
} func (s *MemorySeesionMgr) Init(addr string, options ...string) (err error) {
return
} func (s *MemorySeesionMgr)CreateSession()(session Session,err error) {
s.rwlock.Lock()
defer s.rwlock.Unlock()
// go get github.com/satori/go.uuid
// 用uuid作为sessionId
id, err := uuid.NewV4()
if err != nil{
return
}
// 转string
sessionId := id.String()
// 创建个session
session = NewMemorySession(sessionId) return
} func (s *MemorySeesionMgr)Get(sessionId string)(session Session,err error) {
return
}

  

Gin_Cookie的更多相关文章

  1. Go语言基础之Cookie和Session

    Cookie和Session Cookie和Session是Web开发绕不开的一个环节,本文介绍了Cookie和Session的原理及在Go语言中如何操作Cookie. Cookie Cookie的由 ...

  2. Gin框架中文文档

    Gin 是一个 go 写的 web 框架,具有高性能的优点.官方地址:https://github.com/gin-gonic/gin 带目录请移步 http://xf.shuangdeyu.com/ ...

  3. Go之Cookie和Session

    文章引用自 Cookie和Session Cookie和Session是Web开发绕不开的一个环节,本文介绍了Cookie和Session的原理及在Go语言中如何操作Cookie. Cookie Co ...

  4. gin中设置和获取cookie

    package main import ( "fmt" "github.com/gin-gonic/gin" ) func main() { router := ...

随机推荐

  1. openlayers6结合geoserver利用WFS服务实现图层新增功能(附源码下载)

    内容概览 1.openlayers6结合geoserver利用WFS服务实现图层新增功能2.源代码demo下载 效果图如下: 本篇主要是openlayers6通过调用geoserver发布的地图服务W ...

  2. npm下载文件临时目录、实际存放目录路劲

    npm 下载文件临时存放目录 路劲:C:\Users\xxxxxx\AppData\Roaming\npm\node_modules ( C:\Users\dihongwanyan\AppData\R ...

  3. ELK学习005:Kibana 安装与运行

    下载安装Kibana 1. 下载地址:https://www.elastic.co/cn/downloads/kibana 2. 解压下载的压缩包 [root@localhost ~]# tar -z ...

  4. Redis实现访问控制频率

    为什么限制访问频率 做服务接口时通常需要用到请求频率限制 Rate limiting,例如限制一个用户1分钟内最多可以范围100次 主要用来保证服务性能和保护数据安全 因为如果不进行限制,服务调用者可 ...

  5. Erlang/Elixir精选-第6期(20200113)

    精选文章 Implementing languages on the Erlang VM. -Robert Virding. 因为视频没有显示PPT,PPT可以在点击这里下载. leex - lexi ...

  6. Markdown语法,及其在typora中的快捷键,学写博客吧!!!

    前言 Markdown (MD) 是现在最流行的一种文档书写语言格式.平常写笔记,写博客,写计划再好不过了.个人觉得使用很简单,右击鼠标,有你想要的操作. Typora是简洁.操作简单.功能强大.方便 ...

  7. Nginx虚拟主机配置(20200202)

    一台机器上跑多个站点,即多个域名 curl -xIP:port 域名    用来指定访问的域名在哪个IP的哪个端口上 Nginx默认虚拟主机 不管什么域名解析到该服务器,都会访问到默认虚拟主机 ngi ...

  8. python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程

    自己今天第一次做一个python生日贺卡: 效果图 参考之前有位大佬的作品,自己稍微的改造了一下下  首先:上程序 Birthday.py """ ----------- ...

  9. (LNMP) Nginx_PHP_MariaDB

    L用的是Centos7.5以上,主要是NMP三组件的安装记录. 通常会先安装一下依赖: yum install -y pcre-devel zlib-devel openssl-devel 使用yum ...

  10. python正式学习第二天

    用python操作文件 步骤一:找到文件,打开文件 步骤二:修改文件 ,读取文件 步骤三:关闭文件,并保存 用代码演示如下: 1. f = open(file nama)2. f.read(#读取的字 ...