12345678910111213141516171819202122232425262728293031323334 |
- package middleware
- import (
- "cicv-data-closedloop/common/config/c_log"
- "cicv-data-closedloop/common/entity"
- "github.com/gin-gonic/gin"
- "net/http"
- "strings"
- )
- func ValidateHeaders(whiteList []string, webToken string) gin.HandlerFunc {
- return func(c *gin.Context) {
- // 校验不在白名单中的路由
- if isNotWhite(c.Request.URL.Path, whiteList) {
- acceptToken := c.GetHeader("Authorization")
- if acceptToken == "" || acceptToken != webToken {
- c_log.GlobalLogger.Errorf("请求头 Authorization 校验失败:%v --> %v", acceptToken, webToken)
- c.JSON(http.StatusOK, entity.HttpResult{Status: false, Code: "1001", Message: "请求头 Authorization 校验失败。"})
- c.Abort()
- return
- }
- }
- c.Next()
- }
- }
- func isNotWhite(path string, whiteList []string) bool {
- for _, whitePath := range whiteList {
- if strings.HasPrefix(path, whitePath) {
- return false
- }
- }
- return true
- }
|