作者 yangfu

cors

正在显示 1 个修改的文件 包含 20 行增加0 行删除
@@ -5,6 +5,7 @@ import ( @@ -5,6 +5,7 @@ import (
5 "github.com/astaxie/beego" 5 "github.com/astaxie/beego"
6 "github.com/astaxie/beego/context" 6 "github.com/astaxie/beego/context"
7 beeorm "github.com/astaxie/beego/orm" 7 beeorm "github.com/astaxie/beego/orm"
  8 + "github.com/astaxie/beego/plugins/cors"
8 _ "github.com/go-sql-driver/mysql" 9 _ "github.com/go-sql-driver/mysql"
9 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" 10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
10 "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" 11 "gitlab.fjmaimaimai.com/mmm-go/gocomm/config"
@@ -84,6 +85,9 @@ func main() { @@ -84,6 +85,9 @@ func main() {
84 }() 85 }()
85 log.Info("app on start!") 86 log.Info("app on start!")
86 log.Info("Beego Run Mode:", beego.BConfig.RunMode) 87 log.Info("Beego Run Mode:", beego.BConfig.RunMode)
  88 +
  89 + AllowCors()
  90 +
87 beego.InsertFilter("file/opp/*", beego.BeforeStatic, FilterBeforeStatic) 91 beego.InsertFilter("file/opp/*", beego.BeforeStatic, FilterBeforeStatic)
88 92
89 //https 93 //https
@@ -105,6 +109,22 @@ var FilterBeforeStatic = func(ctx *context.Context) { @@ -105,6 +109,22 @@ var FilterBeforeStatic = func(ctx *context.Context) {
105 } 109 }
106 } 110 }
107 111
  112 +func AllowCors() {
  113 + beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
  114 + //允许访问所有源
  115 + AllowAllOrigins: true,
  116 + //可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
  117 + //其中Options跨域复杂请求预检
  118 + AllowMethods: []string{"*"},
  119 + //指的是允许的Header的种类
  120 + AllowHeaders: []string{"*"},
  121 + //公开的HTTP标头列表
  122 + ExposeHeaders: []string{"Content-Length"},
  123 + //如果设置,则允许共享身份验证凭据,例如cookie
  124 + AllowCredentials: true,
  125 + }))
  126 +}
  127 +
108 func NewBeeormEngine(conf config.Mysql) { 128 func NewBeeormEngine(conf config.Mysql) {
109 aliasName := "default" 129 aliasName := "default"
110 if len(conf.AliasName) > 0 { 130 if len(conf.AliasName) > 0 {