diff --git a/server/config.docker.yaml b/server/config.docker.yaml index a5a72c508924372449a5d462e411f085c38712d3..8466fc05b16a65c269064c9b3a0fc1e0461e9104 100644 --- a/server/config.docker.yaml +++ b/server/config.docker.yaml @@ -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 # 布尔值 diff --git a/server/config.yaml b/server/config.yaml index 97b0bcc60143deb9e518503850e9e1d1b6bd5624..4ad8e3d62003874d556ff5563012e8dbd102b1a4 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -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 # 布尔值 diff --git a/server/config/config.go b/server/config/config.go index 5668edc8500b5db1bb428689ee341ee90fa858d2..dae3571fd7428d54b3a30af0e380210b28630a14 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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"` } diff --git a/server/config/cors.go b/server/config/cors.go new file mode 100644 index 0000000000000000000000000000000000000000..7fba9934697211949bcac3fcd06becdf12f431e5 --- /dev/null +++ b/server/config/cors.go @@ -0,0 +1,14 @@ +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"` +} diff --git a/server/initialize/router.go b/server/initialize/router.go index 473efac8b18305fae0829bf919fe745e7249b65f..853cbb0c12c35ed7623b36a7bb9dd3e012ceb664 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -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") diff --git a/server/middleware/cors.go b/server/middleware/cors.go index fcc4cca4215a32f1c5b511b6f55fe373622ab0d9..99664b7ee4bc9a879e83d72472be51e05fef8d03 100644 --- a/server/middleware/cors.go +++ b/server/middleware/cors.go @@ -1,12 +1,13 @@ package middleware import ( - "net/http" - + "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 @@ -25,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.Request.Method == "GET" && c.Request.URL.Path == "/health") { + 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 +} diff --git a/server/model/system/sys_auto_code.go b/server/model/system/sys_auto_code.go index f31288980a62c756a08c85d83a9e690cb4f749b6..349bec61e7bcf542de86872504257c12984d7c54 100644 --- a/server/model/system/sys_auto_code.go +++ b/server/model/system/sys_auto_code.go @@ -21,7 +21,6 @@ type Field struct { FieldDesc string `json:"fieldDesc"` // 中文名 FieldType string `json:"fieldType"` // Field数据类型 FieldJson string `json:"fieldJson"` // FieldJson - DataType string `json:"dataType"` // 数据库字段类型 DataTypeLong string `json:"dataTypeLong"` // 数据库字段长度 Comment string `json:"comment"` // 数据库字段描述 ColumnName string `json:"columnName"` // 数据库字段 diff --git a/server/resource/template/server/model.go.tpl b/server/resource/template/server/model.go.tpl index fbad1e01bdec36167db76481d885756018d0af4a..e585cc2068bf14d4ffd4b5798453ea0bfa104f72 100644 --- a/server/resource/template/server/model.go.tpl +++ b/server/resource/template/server/model.go.tpl @@ -10,9 +10,9 @@ import ( type {{.StructName}} struct { global.GVA_MODEL {{- range .Fields}} {{- if ne .FieldType "string" }} - {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- end }}"` + {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}"` {{- else }} - {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}}{{- if .DataType -}};type:{{.DataType}}{{- if eq .FieldType "string" -}}{{- if .DataTypeLong -}}({{.DataTypeLong}}){{- end -}}{{- end -}};{{- if ne .FieldType "string" -}}{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}{{- end -}}{{- end -}}"` + {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}"` {{- end }} {{- end }} } diff --git a/server/resource/template/web/table.vue.tpl b/server/resource/template/web/table.vue.tpl index 2c2dde47342e2aa1e22bad93029e72abf73fe280..9cf641d1ad1caef6ac2fd80c066db3f371e054f0 100644 --- a/server/resource/template/web/table.vue.tpl +++ b/server/resource/template/web/table.vue.tpl @@ -22,14 +22,14 @@ {{ end }}{{ end }}{{ end }} - 查询 - 重置 + 查询 + 重置
- 新增 + 新增

确定要删除吗?

@@ -37,7 +37,7 @@ 确定
@@ -69,8 +69,8 @@ {{- end }} diff --git a/web/package.json b/web/package.json index c13bf41b6b8064073d0ae5bd375f12f6a0f846d5..10a1381b987a80a88c3380ba389b519430c262e6 100644 --- a/web/package.json +++ b/web/package.json @@ -24,7 +24,7 @@ "screenfull": "^5.0.2", "script-ext-html-webpack-plugin": "^2.1.4", "spark-md5": "^3.0.1", - "vue": "^3.2.0", + "vue": "^3.2.25", "vue-particle-line": "^0.1.4", "vue-router": "^4.0.0-0", "vuex": "^4.0.0-0", @@ -47,8 +47,8 @@ "eslint-plugin-vue": "^7.0.0", "sass": "^1.26.5", "sass-loader": "^8.0.2", - "vite": "2.5.3", + "vite": "2.5.10", "vite-plugin-banner": "^0.1.3", "vite-plugin-importer": "^0.2.5" } -} \ No newline at end of file +} diff --git a/web/src/view/systemTools/autoCode/component/fieldDialog.vue b/web/src/view/systemTools/autoCode/component/fieldDialog.vue index 18f2c9f31f51919874bbdd830ccca7ae74f3bf45..0661ba8991549156fcd81963f4cde46af22755a2 100644 --- a/web/src/view/systemTools/autoCode/component/fieldDialog.vue +++ b/web/src/view/systemTools/autoCode/component/fieldDialog.vue @@ -40,25 +40,8 @@ /> - - - - - - - - + + diff --git a/web/src/view/systemTools/autoCode/index.vue b/web/src/view/systemTools/autoCode/index.vue index 720b6d38c329eea2d36467044fac1234f817e877..85cc8e5ddaae578babbe31a5d14a1cff668bc702 100644 --- a/web/src/view/systemTools/autoCode/index.vue +++ b/web/src/view/systemTools/autoCode/index.vue @@ -61,7 +61,7 @@ - +