cors.go 644 B

123456789101112131415161718192021
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. // Cors 跨域配置
  7. func Cors(c *gin.Context) {
  8. method := c.Request.Method
  9. c.Header("ACCESS-CONTROL-ALLOW-ORIGIN", "*")
  10. c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
  11. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
  12. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
  13. c.Header("Access-Control-Allow-Credentials", "true")
  14. if method == "OPTIONS" {
  15. c.AbortWithStatus(http.StatusNoContent)
  16. return
  17. }
  18. c.Next()
  19. }