2021-12-16 19:46:16 +08:00
|
|
|
|
#gin_cors
|
|
|
|
|
|
|
|
|
|
##说明
|
|
|
|
|
gin框架的cors跨域处理中间件
|
|
|
|
|
|
|
|
|
|
##使用
|
2022-01-04 10:33:54 +08:00
|
|
|
|
go get github.com/cowardmrx/gin_cors
|
2021-12-16 19:46:16 +08:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
|
|
// 创建cors配置 只要是string类型的一律按照 , 分割,只有AccessControlAllowCredentials是字符串类型的bool值
|
|
|
|
|
cors := &Cors{
|
2022-01-04 10:33:54 +08:00
|
|
|
|
// 允许的域名
|
2021-12-16 19:46:16 +08:00
|
|
|
|
AccessControlAllowOrigins: []string{
|
|
|
|
|
"http://localhost",
|
|
|
|
|
},
|
2022-01-04 10:33:54 +08:00
|
|
|
|
// 允许的请求头 使用 ',' 分割
|
|
|
|
|
AccessControlAllowHeaders: "Authorization",
|
|
|
|
|
// 允许的请求方式 使用 ',' 分割
|
2021-12-16 19:46:16 +08:00
|
|
|
|
AccessControlAllowMethods: "PUT,POST,GET,DELETE",
|
2022-01-04 10:33:54 +08:00
|
|
|
|
// 允许暴露的请求头 使用 ',' 分割
|
|
|
|
|
AccessControlExposeHeaders: "Authorization",
|
|
|
|
|
// 允许凭证记录 true | false
|
|
|
|
|
AccessControlAllowCredentials: 'true'
|
2021-12-16 19:46:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r.POST("/te", func(c *gin.Context) {
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
|
"data": "this is data",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Run(":7878")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|