Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FLIPPED-AURORA
gin-vue-admin
提交
3e1c04c3
G
gin-vue-admin
项目概览
FLIPPED-AURORA
/
gin-vue-admin
大约 1 年 前同步成功
通知
332
Star
18154
Fork
5505
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3e1c04c3
编写于
12月 10, 2021
作者:
S
ShadowWalker
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: CORS组件支持使用配置文件更灵活的配置处理跨域的逻辑;
上级
f3a7c67e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
99 addition
and
4 deletion
+99
-4
server/config.docker.yaml
server/config.docker.yaml
+16
-0
server/config.yaml
server/config.yaml
+15
-1
server/config/config.go
server/config/config.go
+3
-0
server/config/cors.go
server/config/cors.go
+14
-0
server/initialize/router.go
server/initialize/router.go
+3
-2
server/middleware/cors.go
server/middleware/cors.go
+48
-1
未找到文件。
server/config.docker.yaml
浏览文件 @
3e1c04c3
...
...
@@ -135,3 +135,19 @@ Timer:
{
tableName
:
"
sys_operation_records"
,
compareField
:
"
created_at"
,
interval
:
"
2160h"
},
#{ tableName: "log2" , compareField: "created_at", interval: "2160h" }
]
# 跨域配置
# 需要配合 server/initialize/router.go#L32 使用
cors
:
mode
:
whitelist
# 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
whitelist
:
-
allow-origin
:
example1.com
allow-headers
:
content-type
allow-methods
:
GET, POST
expose-headers
:
Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials
:
true
# 布尔值
-
allow-origin
:
example2.com
allow-headers
:
content-type
allow-methods
:
GET, POST
expose-headers
:
Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials
:
true
# 布尔值
server/config.yaml
浏览文件 @
3e1c04c3
...
...
@@ -179,4 +179,18 @@ Timer:
#{ tableName: "log2" , compareField: "created_at", interval: "2160h" }
]
# 跨域配置
# 需要配合 server/initialize/router.go#L32 使用
cors
:
mode
:
whitelist
# 放行模式: allow-all, 放行全部; whitelist, 白名单模式, 来自白名单内域名的请求添加 cors 头; strict-whitelist 严格白名单模式, 白名单外的请求一律拒绝
whitelist
:
-
allow-origin
:
example1.com
allow-headers
:
content-type
allow-methods
:
GET, POST
expose-headers
:
Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials
:
true
# 布尔值
-
allow-origin
:
example2.com
allow-headers
:
content-type
allow-methods
:
GET, POST
expose-headers
:
Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type
allow-credentials
:
true
# 布尔值
server/config/config.go
浏览文件 @
3e1c04c3
...
...
@@ -23,4 +23,7 @@ type Server struct {
Excel
Excel
`mapstructure:"excel" json:"excel" yaml:"excel"`
Timer
Timer
`mapstructure:"timer" json:"timer" yaml:"timer"`
// 跨域配置
Cors
CORS
`mapstructure:"cors" json:"cors" yaml:"cors"`
}
server/config/cors.go
0 → 100644
浏览文件 @
3e1c04c3
package
config
type
CORS
struct
{
Mode
string
`mapstructure:"mode" json:"mode" yaml:"mode"`
Whitelist
[]
CORSWhitelist
`mapstructure:"whitelist" json:"whitelist" yaml:"whitelist"`
}
type
CORSWhitelist
struct
{
AllowOrigin
string
`mapstructure:"allow-origin" json:"allow-origin" yaml:"allow-origin"`
AllowMethods
string
`mapstructure:"allow-methods" json:"allow-methods" yaml:"allow-methods"`
AllowHeaders
string
`mapstructure:"allow-headers" json:"allow-headers" yaml:"allow-headers"`
ExposeHeaders
string
`mapstructure:"expose-headers" json:"expose-headers" yaml:"expose-headers"`
AllowCredentials
bool
`mapstructure:"allow-credentials" json:"allow-credentials" yaml:"allow-credentials"`
}
server/initialize/router.go
浏览文件 @
3e1c04c3
...
...
@@ -29,8 +29,9 @@ func Routers() *gin.Engine {
Router
.
StaticFS
(
global
.
GVA_CONFIG
.
Local
.
Path
,
http
.
Dir
(
global
.
GVA_CONFIG
.
Local
.
Path
))
// 为用户头像和文件提供静态地址
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
global
.
GVA_LOG
.
Info
(
"use middleware logger"
)
// 跨域
//Router.Use(middleware.Cors()) // 如需跨域可以打开
// 跨域,如需跨域可以打开下面的注释
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
//Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
global
.
GVA_LOG
.
Info
(
"use middleware cors"
)
Router
.
GET
(
"/swagger/*any"
,
ginSwagger
.
WrapHandler
(
swaggerFiles
.
Handler
))
global
.
GVA_LOG
.
Info
(
"register swagger handler"
)
...
...
server/middleware/cors.go
浏览文件 @
3e1c04c3
package
middleware
import
(
"github.com/flipped-aurora/gin-vue-admin/server/config"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/gin-gonic/gin"
"net/http"
)
//
处理跨域请求,支持options访问
//
Cors 直接放行所有跨域请求并放行所有 OPTIONS 方法
func
Cors
()
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
method
:=
c
.
Request
.
Method
...
...
@@ -24,3 +26,48 @@ func Cors() gin.HandlerFunc {
c
.
Next
()
}
}
// CorsByRules 按照配置处理跨域请求
func
CorsByRules
()
gin
.
HandlerFunc
{
// 放行全部
if
global
.
GVA_CONFIG
.
Cors
.
Mode
==
"allow-all"
{
return
Cors
()
}
return
func
(
c
*
gin
.
Context
)
{
whitelist
:=
checkCors
(
c
.
GetHeader
(
"origin"
))
// 通过检查, 添加请求头
if
whitelist
!=
nil
{
c
.
Header
(
"Access-Control-Allow-Origin"
,
whitelist
.
AllowOrigin
)
c
.
Header
(
"Access-Control-Allow-Headers"
,
whitelist
.
AllowHeaders
)
c
.
Header
(
"Access-Control-Allow-Methods"
,
whitelist
.
AllowMethods
)
c
.
Header
(
"Access-Control-Expose-Headers"
,
whitelist
.
ExposeHeaders
)
if
whitelist
.
AllowCredentials
{
c
.
Header
(
"Access-Control-Allow-Credentials"
,
"true"
)
}
}
// 严格白名单模式且未通过检查,直接拒绝处理请求
if
whitelist
==
nil
&&
global
.
GVA_CONFIG
.
Cors
.
Mode
==
"strict-whitelist"
{
c
.
AbortWithStatus
(
http
.
StatusForbidden
)
}
else
{
// 非严格白名单模式,无论是否通过检查均放行所有 OPTIONS 方法
if
c
.
Request
.
Method
==
"OPTIONS"
{
c
.
AbortWithStatus
(
http
.
StatusNoContent
)
}
}
// 处理请求
c
.
Next
()
}
}
func
checkCors
(
currentOrigin
string
)
*
config
.
CORSWhitelist
{
for
_
,
whitelist
:=
range
global
.
GVA_CONFIG
.
Cors
.
Whitelist
{
// 遍历配置中的跨域头,寻找匹配项
if
currentOrigin
==
whitelist
.
AllowOrigin
{
return
&
whitelist
}
}
return
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录