token.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package middleware
  2. import (
  3. "eta_gn/eta_bridge/controller/resp"
  4. "eta_gn/eta_bridge/global"
  5. "eta_gn/eta_bridge/utils"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func Token() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. token := c.Request.Header.Get("Authorization")
  12. if token == "" {
  13. token = c.DefaultQuery("authorization", "")
  14. if token == "" {
  15. token = c.DefaultQuery("Authorization", "")
  16. }
  17. }
  18. if token == "" {
  19. resp.TokenError(nil, "非法访问", "非法访问", c)
  20. c.Abort()
  21. return
  22. }
  23. if token != utils.CrmEtaAuthorization {
  24. resp.TokenError(nil, "非法访问", "非法访问", c)
  25. c.Abort()
  26. return
  27. }
  28. c.Next()
  29. }
  30. }
  31. func InternalToken() gin.HandlerFunc {
  32. return func(c *gin.Context) {
  33. token := c.Request.Header.Get("Authorization")
  34. if token == "" {
  35. token = c.DefaultQuery("authorization", "")
  36. if token == "" {
  37. token = c.DefaultQuery("Authorization", "")
  38. }
  39. }
  40. if token == "" {
  41. resp.TokenError(nil, "非法访问", "非法访问", c)
  42. c.Abort()
  43. return
  44. }
  45. checkToken := utils.MD5(global.CONFIG.Serve.AppNameEn + global.CONFIG.Serve.Md5Key)
  46. if token != checkToken {
  47. resp.TokenError(nil, "非法访问", "非法访问", c)
  48. c.Abort()
  49. return
  50. }
  51. utils.SetBridgeLogListByClaims(c, fmt.Sprint("Token:", token))
  52. c.Next()
  53. }
  54. }