Skip to content

Commit b6f3038

Browse files
andywu1998奇淼(piexlmax
andauthored
bugfix: 通过修改配置文件使得跨域白名单正常生效 (#1296)
* bugfix: 通过修改配置文件使得跨域白名单正常生效 Co-authored-by: 奇淼(piexlmax <[email protected]>
1 parent e67354f commit b6f3038

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

server/config.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,16 @@ Timer:
187187
# 跨域配置
188188
# 需要配合 server/initialize/router.go#L32 使用
189189
cors:
190-
mode: whitelist # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
190+
mode: strict-whitelist # 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
191191
whitelist:
192192
- allow-origin: example1.com
193-
allow-headers: content-type
194-
allow-methods: GET, POST
193+
allow-headers: Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id
194+
allow-methods: POST, GET
195195
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
196+
196197
allow-credentials: true # 布尔值
197198
- allow-origin: example2.com
198199
allow-headers: content-type
199200
allow-methods: GET, POST
200201
expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
201-
allow-credentials: true # 布尔值
202+
allow-credentials: true # 布尔值

server/initialize/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Routers() *gin.Engine {
3131
// Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
3232
// 跨域,如需跨域可以打开下面的注释
3333
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
34-
//Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
34+
// Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
3535
//global.GVA_LOG.Info("use middleware cors")
3636
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
3737
global.GVA_LOG.Info("register swagger handler")

server/middleware/cors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func CorsByRules() gin.HandlerFunc {
5252
c.AbortWithStatus(http.StatusForbidden)
5353
} else {
5454
// 非严格白名单模式,无论是否通过检查均放行所有 OPTIONS 方法
55-
if c.Request.Method == "OPTIONS" {
55+
if c.Request.Method == http.MethodOptions {
5656
c.AbortWithStatus(http.StatusNoContent)
5757
}
5858
}

0 commit comments

Comments
 (0)