2 Star 0 Fork 1

web-bird / bird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sign.go 960 Bytes
一键复制 编辑 原始数据 按行查看 历史
悟道人 提交于 2020-06-11 13:47 . 保存
package middleware
import (
"fmt"
"gitee.com/web-bird/bird/assets/utils"
"sync"
"github.com/gin-gonic/gin"
)
//签名验证中间件
//签名长度在74-104字符之间
//signKey=sign的header key名
//secret=密匙
var signsMap = new(sync.Map)
var signLength = 0
func Sign(signKey, secret string) gin.HandlerFunc { //
return func(c *gin.Context) {
signLength++
if signLength > 100000 {
signsMap = new(sync.Map)
signLength = 0
}
signStr := c.GetHeader(signKey)
if l := len(signStr); l < 74 || l > 104 {
c.AbortWithStatus(403)
return
}
signIndex := utils.Sha256(c.ClientIP(), c.Request.RequestURI, signStr)
if _, ok := signsMap.Load(signIndex); ok {
c.AbortWithStatus(403)
return
}
start := len(signStr) - 64
token := signStr[:start]
sign := signStr[start:]
if utils.Sha256(fmt.Sprint(token, secret)) != sign { //签名未通过
c.AbortWithStatus(403)
return
}
signsMap.Store(signIndex, true)
}
}
1
https://gitee.com/web-bird/bird.git
git@gitee.com:web-bird/bird.git
web-bird
bird
bird
eddb2f01d39e

搜索帮助