作者 陈志颖

feat:添加跨域

@@ -5,18 +5,19 @@ go 1.14 @@ -5,18 +5,19 @@ go 1.14
5 require ( 5 require (
6 github.com/Shopify/sarama v1.26.4 6 github.com/Shopify/sarama v1.26.4
7 github.com/ajg/form v1.5.1 // indirect 7 github.com/ajg/form v1.5.1 // indirect
8 - github.com/astaxie/beego v1.12.1 8 + github.com/astaxie/beego v1.12.3
9 github.com/dgrijalva/jwt-go v3.2.0+incompatible 9 github.com/dgrijalva/jwt-go v3.2.0+incompatible
  10 + github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
10 github.com/gavv/httpexpect v2.0.0+incompatible 11 github.com/gavv/httpexpect v2.0.0+incompatible
11 github.com/gin-gonic/gin v1.5.0 12 github.com/gin-gonic/gin v1.5.0
12 github.com/go-pg/pg/v10 v10.0.0-beta.2 13 github.com/go-pg/pg/v10 v10.0.0-beta.2
13 github.com/imkira/go-interpol v1.1.0 // indirect 14 github.com/imkira/go-interpol v1.1.0 // indirect
  15 + github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
14 github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9 16 github.com/linmadan/egglib-go v0.0.0-20191217144343-ca4539f95bf9
15 github.com/moul/http2curl v1.0.0 // indirect 17 github.com/moul/http2curl v1.0.0 // indirect
16 github.com/onsi/ginkgo v1.15.0 18 github.com/onsi/ginkgo v1.15.0
17 github.com/onsi/gomega v1.10.5 19 github.com/onsi/gomega v1.10.5
18 github.com/sergi/go-diff v1.1.0 // indirect 20 github.com/sergi/go-diff v1.1.0 // indirect
19 - github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect  
20 github.com/shopspring/decimal v1.2.0 21 github.com/shopspring/decimal v1.2.0
21 github.com/tiptok/gocomm v1.0.5 22 github.com/tiptok/gocomm v1.0.5
22 github.com/valyala/fasthttp v1.19.0 // indirect 23 github.com/valyala/fasthttp v1.19.0 // indirect
@@ -24,4 +25,5 @@ require ( @@ -24,4 +25,5 @@ require (
24 github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect 25 github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
25 github.com/yudai/gojsondiff v1.0.0 // indirect 26 github.com/yudai/gojsondiff v1.0.0 // indirect
26 github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect 27 github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
  28 + github.com/yudai/pp v2.0.1+incompatible // indirect
27 ) 29 )
@@ -9,6 +9,8 @@ import ( @@ -9,6 +9,8 @@ import (
9 ) 9 )
10 10
11 func init() { 11 func init() {
  12 + beego.InsertFilter("/*", beego.BeforeRouter, middleware.AllowCors())
  13 +
12 beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequestBodyFilter()) 14 beego.InsertFilter("/*", beego.BeforeExec, middleware.CreateRequestBodyFilter())
13 15
14 beego.InsertFilter("/v1/*", beego.BeforeExec, middleware.CheckJWTToken) 16 beego.InsertFilter("/v1/*", beego.BeforeExec, middleware.CheckJWTToken)
  1 +/**
  2 + @author: stevechan
  3 + @date: 2021/3/9
  4 + @note:
  5 +**/
  6 +
  7 +package middleware
  8 +
  9 +import (
  10 + "github.com/astaxie/beego/context"
  11 + "github.com/astaxie/beego/plugins/cors"
  12 +)
  13 +
  14 +func AllowCors() func(ctx *context.Context) {
  15 + fn := cors.Allow(&cors.Options{
  16 + //允许访问所有源
  17 + AllowAllOrigins: true,
  18 + //可选参数"GET", "POST", "PUT", "DELETE", "OPTIONS" (*为所有)
  19 + //其中Options跨域复杂请求预检
  20 + AllowMethods: []string{"*"},
  21 + //指的是允许的Header的种类
  22 + AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Content-Type", "x-requested-with"},
  23 + //公开的HTTP标头列表
  24 + ExposeHeaders: []string{"Content-Length"},
  25 + //如果设置,则允许共享身份验证凭据,例如cookie
  26 + AllowCredentials: true,
  27 + })
  28 + return fn
  29 +}