123456789101112131415161718192021222324252627 |
- package middleware
- import (
- beego "github.com/beego/beego/v2/adapter"
- "github.com/beego/beego/v2/adapter/context"
- "net/http"
- )
- var success = []byte("SUPPORT OPTIONS")
- var corsFunc = func(ctx *context.Context) {
- //origin := ctx.Input.Header("Origin")
- ctx.Output.Header("Access-Control-Allow-Methods", "OPTIONS,DELETE,POST,GET,PUT,PATCH")
- ctx.Output.Header("Access-Control-Max-Age", "3600")
- ctx.Output.Header("Access-Control-Allow-Headers", "X-Custom-Header,accept,Content-Type,Access-Token,Authorization")
- ctx.Output.Header("Access-Control-Expose-Headers", "Content-Disposition")
- ctx.Output.Header("Access-Control-Allow-Credentials", "true")
- ctx.Output.Header("Access-Control-Allow-Origin", "*")
- if ctx.Input.Method() == http.MethodOptions {
- ctx.Output.SetStatus(http.StatusOK)
- _ = ctx.Output.Body(success)
- }
- }
- func CorsMiddle() {
- beego.InsertFilter("/*", beego.BeforeRouter, corsFunc)
- }
|