diff --git a/cmd/server.go b/cmd/server.go index 84f8eb0..7ec22f3 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -56,6 +56,7 @@ func run() { engine.LoadHTMLGlob("static/html/*") engine.Static("/static", "./static") engine.Use(tools.Session("gofly")) + engine.Use(middleware.CrossSite) //性能监控 pprof.Register(engine) diff --git a/middleware/cross.go b/middleware/cross.go new file mode 100644 index 0000000..953b189 --- /dev/null +++ b/middleware/cross.go @@ -0,0 +1,16 @@ +package middleware + +import "github.com/gin-gonic/gin" + +func CrossSite(c *gin.Context) { + c.Writer.Header().Set("Access-Control-Allow-Origin", "*") + //服务器支持的所有跨域请求的方法 + c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") + //允许跨域设置可以返回其他子段,可以自定义字段 + c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session") + // 允许浏览器(客户端)可以解析的头部 (重要) + c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers") + //允许客户端传递校验信息比如 cookie (重要) + c.Header("Access-Control-Allow-Credentials", "true") + c.Next() +} diff --git a/static/js/gofly-front.js b/static/js/gofly-front.js index eff6249..c7e5283 100644 --- a/static/js/gofly-front.js +++ b/static/js/gofly-front.js @@ -174,7 +174,10 @@ GOFLY.getNotice=function(){ "×" + ""; welcomeHtml+="