middle.go
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package middleware
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"oppmg/common/log"
"oppmg/protocol"
"strings"
"github.com/astaxie/beego/context"
)
//LogRequestData Before Router
var LogRequestData = func(ctx *context.Context) {
log.Info("====>Recv Request:%s", ctx.Input.URI())
if ctx.Input.RequestBody != nil {
log.Info("====>Recv data from client:\nHeadData: %s \nBodyData: %s", ctx.Request.Header, string(ctx.Input.RequestBody))
} else {
log.Info("====>Recv data from client:\nHeadData: %s ", ctx.Request.Header)
}
}
//CheckSign Before Router
var CheckSign = func(ctx *context.Context) {
var (
headTimeStamp string
headUuid string
headAccessToken string
headSign string
signHex string
)
headTimeStamp = ctx.Input.Header(protocol.HeaderTimestamp)
headUuid = ctx.Input.Header(protocol.HeaderUUID)
headSign = ctx.Input.Header(protocol.HeaderSign)
setsign := fmt.Sprintf("v!(MmM%v%v%vMmM)i^", headTimeStamp, headUuid, headAccessToken)
sha256 := sha256.New()
sha256.Write([]byte(setsign))
signHex = hex.EncodeToString(sha256.Sum(nil))
if strings.Compare(signHex, headSign) != 0 {
msg := protocol.BadRequestParam("113")
ctx.Output.JSON(msg, false, false)
return
}
return
}
//AuthToken Before Router
var AuthToken = func(ctx *context.Context) {
log.Debug("执行中间件AuthToken")
// ctx.Output.Body([]byte("{}"))
return
}
//AppSwitch 使用外部变量停止处理新的请求
var AppSwitch = func(ctx *context.Context) {
return
}