Skip to content

Commit 54a375e

Browse files
author
奇淼(piexlmax
authored
Merge pull request flipped-aurora#837 from ShadowWaIker/pgsqlDevelop
完善CORS组件
2 parents f3a7c67 + e01d52b commit 54a375e

File tree

6 files changed

+99
-4
lines changed

6 files changed

+99
-4
lines changed

server/config.docker.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,19 @@ Timer:
135135
{ tableName: "sys_operation_records" , compareField: "created_at", interval: "2160h" },
136136
#{ tableName: "log2" , compareField: "created_at", interval: "2160h" }
137137
]
138+
139+
# 跨域配置
140+
# 需要配合 server/initialize/router.go#L32 使用
141+
cors:
142+
mode: whitelist # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
143+
whitelist:
144+
- allow-origin: example1.com
145+
allow-headers: content-type
146+
allow-methods: GET, POST
147+
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
148+
allow-credentials: true # 布尔值
149+
- allow-origin: example2.com
150+
allow-headers: content-type
151+
allow-methods: GET, POST
152+
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
153+
allow-credentials: true # 布尔值

server/config.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,18 @@ Timer:
179179
#{ tableName: "log2" , compareField: "created_at", interval: "2160h" }
180180
]
181181

182-
182+
# 跨域配置
183+
# 需要配合 server/initialize/router.go#L32 使用
184+
cors:
185+
mode: whitelist # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
186+
whitelist:
187+
- allow-origin: example1.com
188+
allow-headers: content-type
189+
allow-methods: GET, POST
190+
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
191+
allow-credentials: true # 布尔值
192+
- allow-origin: example2.com
193+
allow-headers: content-type
194+
allow-methods: GET, POST
195+
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
196+
allow-credentials: true # 布尔值

server/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ type Server struct {
2323

2424
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
2525
Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`
26+
27+
// 跨域配置
28+
Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
2629
}

server/config/cors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package config
2+
3+
type CORS struct {
4+
Mode string `mapstructure:"mode" json:"mode" yaml:"mode"`
5+
Whitelist []CORSWhitelist `mapstructure:"whitelist" json:"whitelist" yaml:"whitelist"`
6+
}
7+
8+
type CORSWhitelist struct {
9+
AllowOrigin string `mapstructure:"allow-origin" json:"allow-origin" yaml:"allow-origin"`
10+
AllowMethods string `mapstructure:"allow-methods" json:"allow-methods" yaml:"allow-methods"`
11+
AllowHeaders string `mapstructure:"allow-headers" json:"allow-headers" yaml:"allow-headers"`
12+
ExposeHeaders string `mapstructure:"expose-headers" json:"expose-headers" yaml:"expose-headers"`
13+
AllowCredentials bool `mapstructure:"allow-credentials" json:"allow-credentials" yaml:"allow-credentials"`
14+
}

server/initialize/router.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ func Routers() *gin.Engine {
2929
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.Path)) // 为用户头像和文件提供静态地址
3030
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
3131
global.GVA_LOG.Info("use middleware logger")
32-
// 跨域
33-
//Router.Use(middleware.Cors()) // 如需跨域可以打开
32+
// 跨域,如需跨域可以打开下面的注释
33+
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
34+
//Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
3435
global.GVA_LOG.Info("use middleware cors")
3536
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
3637
global.GVA_LOG.Info("register swagger handler")

server/middleware/cors.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package middleware
22

33
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/config"
5+
"github.com/flipped-aurora/gin-vue-admin/server/global"
46
"github.com/gin-gonic/gin"
57
"net/http"
68
)
79

8-
// 处理跨域请求,支持options访问
10+
// Cors 直接放行所有跨域请求并放行所有 OPTIONS 方法
911
func Cors() gin.HandlerFunc {
1012
return func(c *gin.Context) {
1113
method := c.Request.Method
@@ -24,3 +26,48 @@ func Cors() gin.HandlerFunc {
2426
c.Next()
2527
}
2628
}
29+
30+
// CorsByRules 按照配置处理跨域请求
31+
func CorsByRules() gin.HandlerFunc {
32+
// 放行全部
33+
if global.GVA_CONFIG.Cors.Mode == "allow-all" {
34+
return Cors()
35+
}
36+
return func(c *gin.Context) {
37+
whitelist := checkCors(c.GetHeader("origin"))
38+
39+
// 通过检查, 添加请求头
40+
if whitelist != nil {
41+
c.Header("Access-Control-Allow-Origin", whitelist.AllowOrigin)
42+
c.Header("Access-Control-Allow-Headers", whitelist.AllowHeaders)
43+
c.Header("Access-Control-Allow-Methods", whitelist.AllowMethods)
44+
c.Header("Access-Control-Expose-Headers", whitelist.ExposeHeaders)
45+
if whitelist.AllowCredentials {
46+
c.Header("Access-Control-Allow-Credentials", "true")
47+
}
48+
}
49+
50+
// 严格白名单模式且未通过检查,直接拒绝处理请求
51+
if whitelist == nil && global.GVA_CONFIG.Cors.Mode == "strict-whitelist" && !(c.Request.Method == "GET" && c.Request.URL.Path == "/health") {
52+
c.AbortWithStatus(http.StatusForbidden)
53+
} else {
54+
// 非严格白名单模式,无论是否通过检查均放行所有 OPTIONS 方法
55+
if c.Request.Method == "OPTIONS" {
56+
c.AbortWithStatus(http.StatusNoContent)
57+
}
58+
}
59+
60+
// 处理请求
61+
c.Next()
62+
}
63+
}
64+
65+
func checkCors(currentOrigin string) *config.CORSWhitelist {
66+
for _, whitelist := range global.GVA_CONFIG.Cors.Whitelist {
67+
// 遍历配置中的跨域头,寻找匹配项
68+
if currentOrigin == whitelist.AllowOrigin {
69+
return &whitelist
70+
}
71+
}
72+
return nil
73+
}

0 commit comments

Comments
 (0)