m_validate_header.go 917 B

12345678910111213141516171819202122232425262728293031323334
  1. package middleware
  2. import (
  3. "cicv-data-closedloop/common/config/c_log"
  4. "cicv-data-closedloop/common/entity"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. "strings"
  8. )
  9. func ValidateHeaders(whiteList []string, webToken string) gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. // 校验不在白名单中的路由
  12. if isNotWhite(c.Request.URL.Path, whiteList) {
  13. acceptToken := c.GetHeader("Authorization")
  14. if acceptToken == "" || acceptToken != webToken {
  15. c_log.GlobalLogger.Errorf("请求头 Authorization 校验失败:%v --> %v", acceptToken, webToken)
  16. c.JSON(http.StatusOK, entity.HttpResult{Status: false, Code: "1001", Message: "请求头 Authorization 校验失败。"})
  17. c.Abort()
  18. return
  19. }
  20. }
  21. c.Next()
  22. }
  23. }
  24. func isNotWhite(path string, whiteList []string) bool {
  25. for _, whitePath := range whiteList {
  26. if strings.HasPrefix(path, whitePath) {
  27. return false
  28. }
  29. }
  30. return true
  31. }