当前的代码是为了golang做权限验证和参数记录的代码
代码 使用拦截器 router := gin.New()
routerGroup := router.Group("/interceptor")
routerGroup.Use(interceptor.HttpInterceptor())
拦截器逻辑
package interceptor
import (
"bytes"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http"
)
// http 请求拦截器
func HttpInterceptor() gin.HandlerFunc {
return func(c *gin.Context) {
// 签名校验逻辑,逻辑调用自己的签名逻辑即可
// checkSign := signature.CheckSign(c)
// if !checkSign {
// c.Abort()
// return
// }
request := c.Request
_ = request.ParseForm()
// 获取参数值
req, ok := getReqParam(c, request)
if ok {
return
}
// 获取响应结果
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer}
c.Request = request
c.Writer = blw
// 执行下一个
c.Next()
// 获取 响应
realIp := util.GetRealIp(request)
log.Info("请求地址:", c.FullPath(), ",请求ip:", realIp, ",Referer:", request.Referer(), ",请求参数:", req, ",响应参数:", blw.body.String())
}
}
func getReqParam(c *gin.Context, request *http.Request) (string, bool) {
req := request.Form.Encode()
// if(len(strings.Trim(req,"")) <= 0)
if !util.IsBlank(req) {
return req, false
}
// 获取body的值,后面的请求参数消失,暂时注释
//body := request.Body
//data, err := ioutil.ReadAll(body)
获取请求
data, err := c.GetRawData()
//if err != nil {
// // 执行下一个
// c.Next()
// return "", true
//}
//req = string(data)
return "", false
}
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)