gin_cros.go 762 B

123456789101112131415161718192021
  1. package http_middleware
  2. import "github.com/gin-gonic/gin"
  3. // 允许跨域响应头中间件
  4. func AddCrossOriginHeaders() gin.HandlerFunc {
  5. return func(ctxt *gin.Context) {
  6. ctxt.Header("Access-Control-Allow-Credentials", "true")
  7. var origin string
  8. if ctxt.Request.Header.Get("Origin") != "" {
  9. origin = ctxt.Request.Header.Get("Origin")
  10. } else {
  11. origin = "*"
  12. }
  13. ctxt.Header("Access-Control-Allow-Origin", origin)
  14. ctxt.Header("Access-Control-Max-Age", "3600")
  15. ctxt.Header("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT")
  16. ctxt.Header("Access-Control-Allow-Headers", "Origin,x-requested-with,If-Modified-Since,Pragma,Last-Modified,Cache-Control,Expires,Content-Type,X-E4M-With,Accept,Authorization,Platform")
  17. ctxt.Next()
  18. }
  19. }