修复跨域问题,修改一些问题

This commit is contained in:
jiayuqi7813
2022-07-20 22:29:16 +08:00
parent d660b5ec00
commit 0018654920
3 changed files with 38 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ import (
func Initrouter() {
router := gin.Default()
router.Use(Cors())
api_v1 := router.Group("/api/v1")
//公共接口(无需登录)
@@ -93,3 +94,24 @@ func Initrouter() {
return
}
}
// 跨域
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin") //请求头部
if origin != "" {
c.Header("Access-Control-Allow-Origin", origin) //"http://172.20.10.10:8081")
c.Header("Access-Control-Allow-Headers", "Content-Category, AccessToken, X-CSRF-Token, Authorization, Token, Content-Type")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PATCH, DELETE, PUT")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Category")
c.Header("Access-Control-Max-Age", "172800")
c.Header("Access-Control-Allow-Credentials", "true")
}
if method == "OPTIONS" {
c.JSON(200, "ok")
}
// 处理请求
c.Next()
}
}