Skip to content

Commit

Permalink
Add 路由中间件 MD5 签名
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Sep 30, 2019
1 parent 5ae8afb commit dc613b9
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const (
AppPort = ":9999"
AppName = "go-gin-api"

// MD5 密钥
AppSignSecret = "4OhYXtDYNYxQsGetqASVOTP37jGt5gGY"

// MD5 签名超时时间 120s
AppSignExpiry = "120"


// 超时时间
AppReadTimeout = 120
AppWriteTimeout = 120
Expand Down
107 changes: 107 additions & 0 deletions app/route/middleware/sign/md5/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package sign_md5

import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"go-gin-api/app/config"
"go-gin-api/app/util"
"net/url"
"sort"
"strconv"
"time"
)

// MD5 组合加密
func SetUp() gin.HandlerFunc {

return func(c *gin.Context) {
utilGin := util.Gin{Ctx: c}

sign, err := verifyMD5Sign(c)

if sign != nil {
utilGin.Response(-1, "Debug Sign", sign)
c.Abort()
return
}

if err != nil {
utilGin.Response(-1, err.Error(), sign)
c.Abort()
return
}

c.Next()
}
}

// 创建签名
func createMD5Sign(params url.Values) string {
var key []string
var str = ""
for k := range params {
if k != "sn" && k != "ts" && k != "debug" {
key = append(key, k)
}
}
sort.Strings(key)
for i := 0; i < len(key); i++ {
if i == 0 {
str = fmt.Sprintf("%v=%v", key[i], params.Get(key[i]))
} else {
str = str + fmt.Sprintf("&%v=%v", key[i], params.Get(key[i]))
}
}

// 自定义签名算法
sign := util.MD5(config.AppSignSecret + str + config.AppSignSecret)
return sign
}

// 验证签名
func verifyMD5Sign(c *gin.Context) (map[string]string, error) {
var method = c.Request.Method
var ts int64
var sn string
var req url.Values
var debug string

if method == "GET" {
req = c.Request.URL.Query()
sn = c.Query("sn")
debug = c.Query("debug")
ts, _ = strconv.ParseInt(c.Query("ts"), 10, 64)
} else if method == "POST" {
_ = c.Request.ParseForm()
req = c.Request.PostForm
sn = c.PostForm("sn")
debug = c.PostForm("debug")
ts, _ = strconv.ParseInt(c.PostForm("ts"), 10, 64)
} else {
return nil, errors.New("非法请求")
}

if debug == "1" {
res := map[string]string{
"ts": strconv.FormatInt(util.GetCurrentUnix(), 10),
"sn": createMD5Sign(req),
}
return res, nil
}

exp, _ := strconv.ParseInt(config.AppSignExpiry, 10, 64)

// 验证过期时间
timestamp := time.Now().Unix()
if ts > timestamp || timestamp - ts >= exp {
return nil, errors.New("ts Error")
}

// 验证签名
if sn == "" || sn != createMD5Sign(req) {
return nil, errors.New("sn Error")
}

return nil, nil
}
3 changes: 2 additions & 1 deletion app/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go-gin-api/app/route/middleware/exception"
"go-gin-api/app/route/middleware/jaeger"
"go-gin-api/app/route/middleware/logger"
signMD5 "go-gin-api/app/route/middleware/sign/md5"
"go-gin-api/app/util"
)

Expand All @@ -31,7 +32,7 @@ func SetupRouter(engine *gin.Engine) {

//@todo 记录请求超时的路由

ProductRouter := engine.Group("/product")
ProductRouter := engine.Group("/product").Use(signMD5.SetUp())
{
// 新增产品
ProductRouter.POST("", product.Add)
Expand Down
12 changes: 12 additions & 0 deletions app/util/md5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package util

import (
"crypto/md5"
"encoding/hex"
)

func MD5(str string) string {
s := md5.New()
s.Write([]byte(str))
return hex.EncodeToString(s.Sum(nil))
}

0 comments on commit dc613b9

Please sign in to comment.