1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| package middleware
import "net/http"
// CorsMiddleware 跨域请求处理中间件 type CorsMiddleware struct { }
// NewCorsMiddleware 新建跨域请求处理中间件 func NewCorsMiddleware() *CorsMiddleware { return &CorsMiddleware{} }
// Handle 跨域请求处理 func (m *CorsMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { setHeader(w)
// 放行所有 OPTIONS 方法 if r.Method == "OPTIONS" { w.WriteHeader(http.StatusNoContent) return }
// 处理请求 next(w, r) } }
// Handler 跨域请求处理器 func (m *CorsMiddleware) Handler() http.Handler { return http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { setHeader(w)
if r.Method == "OPTIONS" { w.WriteHeader(http.StatusNoContent) } else { w.WriteHeader(http.StatusNotFound) } }, ) }
// setHeader 设置响应头 func setHeader(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set( "Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, Authorization, AccessToken, Token, Lang, CLIENT-ID", ) w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH") w.Header().Set( "Access-Control-Expose-Headers", "Content-Length, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers", ) w.Header().Set("Access-Control-Allow-Credentials", "true") }
|