12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package middleware
- import (
- "backend-linksee-api/common"
- "backend-linksee-api/utils"
- "github.com/gin-contrib/cors"
- "github.com/gin-gonic/gin"
- "strings"
- "time"
- )
- // CORSMiddleware 跨域请求中间件
- func CORSMiddleware() gin.HandlerFunc {
- return cors.New(cors.Config{
- AllowOrigins: []string{"*"},
- AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
- AllowHeaders: []string{"Origin, x-requested-with, Content-Type, X-Token,Authorization"},
- AllowCredentials: true,
- MaxAge: time.Second * time.Duration(7200),
- })
- }
- //func RecoredReq()gin.HandlerFunc {
- // return func(c *gin.Context) {
- // var postArgs = ""
- // var method = ""
- // if c.Request.Method == "post" {
- // data, _ := ioutil.ReadAll(c.Request.Body)
- // postArgs = string(data)
- // method = "post"
- // }else{
- // method = "get"
- // }
- // sql := `insert into btk_request_log(request,method,args)values(?,?,?)`
- // db, err := utils.OpenConnection()
- // if err != nil {
- // utils.LZPrint("RecoredReqErr",err.Error())
- // }
- // defer db.Close()
- // db.Exec(sql,c.getp)
- // }
- //}
- func ValidToken() gin.HandlerFunc {
- return func(c *gin.Context) {
- path := c.Request.URL.Path
- if strings.Index(path, "/order/pay/notify") == 0 || strings.Index(path, "login") != -1 || strings.Index(path, "swagger") != -1 || strings.Index(path, "panic") != -1 {
- //fmt.Println("path is:",path,strings.Index(path, "/order/pay/notify"))
- c.Next()
- } else {
- //fmt.Println("auth")
- tokens := strings.Replace(c.GetHeader("authorization"), "Bearer ", "", -1)
- if tokens == "" {
- common.ReturnFailure(c, 409, "token不能空", nil)
- c.Abort()
- return
- }
- _, err8 := utils.ParseToken(tokens)
- if err8 != nil {
- common.ReturnFailure(c, 409, err8.Error(), nil)
- c.Abort()
- }
- }
- }
- }
|