package core import ( "github.com/gin-gonic/gin" "strings" ) func CorsMiddleWare() gin.HandlerFunc { return func(context *gin.Context) { method := context.Request.Method origin := context.Request.Header.Get("Origin") //请求头部 if origin != "" { //接收客户端发送的origin (重要!) context.Writer.Header().Set("Access-Control-Allow-Origin", "*") //服务器支持的所有跨域请求的方法 context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") //允许跨域设置可以返回其他子段,可以自定义字段 context.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session") // 允许浏览器(客户端)可以解析的头部 (重要) context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers") //设置缓存时间 //c.Header("Access-Control-Max-Age", "172800") //允许客户端传递校验信息比如 cookie (重要) context.Header("Access-Control-Allow-Credentials", "true") } //允许类型校验 if method == "OPTIONS" { context.AbortWithStatus(200) } else { context.Next() } } } func RouteReplace(router *gin.Engine, routeStr string) gin.HandlerFunc { return func(context *gin.Context) { // 去掉 前缀 newPath := strings.TrimPrefix(context.Request.URL.Path, routeStr) if newPath == context.Request.URL.Path { // 如果没有匹配到前缀,直接放行 context.Next() return } if newPath == "" { newPath = "/" } // 修改请求路径并重新处理 context.Request.URL.Path = newPath router.HandleContext(context) context.Abort() } }