cors_middle.go 908 B

123456789101112131415161718192021222324252627
  1. package middleware
  2. import (
  3. beego "github.com/beego/beego/v2/adapter"
  4. "github.com/beego/beego/v2/adapter/context"
  5. "net/http"
  6. )
  7. var success = []byte("SUPPORT OPTIONS")
  8. var corsFunc = func(ctx *context.Context) {
  9. //origin := ctx.Input.Header("Origin")
  10. ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
  11. ctx.Output.Header("Access-Control-Max-Age", "3600")
  12. ctx.Output.Header("Access-Control-Allow-Headers", "X-Custom-Header,accept,Content-Type,Access-Token,Authorization")
  13. ctx.Output.Header("Access-Control-Expose-Headers", "Content-Disposition")
  14. ctx.Output.Header("Access-Control-Allow-Credentials", "true")
  15. ctx.Output.Header("Access-Control-Allow-Origin", "*")
  16. if ctx.Input.Method() == http.MethodOptions {
  17. ctx.Output.SetStatus(http.StatusOK)
  18. _ = ctx.Output.Body(success)
  19. }
  20. }
  21. func CorsMiddle() {
  22. beego.InsertFilter("/*", beego.BeforeRouter, corsFunc)
  23. }