qwe*_*max 16 frameworks go cors preflight go-gin
我正在使用Go gin框架杜松子酒
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有状态码:200 OK,但OPTIONS请求后没有任何反应.看起来我想念一些东西,但我无法理解我错在哪里.
有谁能够帮我?
Jac*_*ack 29
FWIW,这是我的CORS中间件,可以满足我的需求.
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
Run Code Online (Sandbox Code Playgroud)
小智 18
我花了大约一个小时来了解为什么互联网上的一些示例有效,而有些则无效。所以我明白了区别 - 行顺序很重要,首先你应该使用配置,然后声明你的端点,但不是相反的方式。
作品:
router := gin.Default()
router.Use(cors.Default())
router.GET("/ping", pong)
router.Run(":8082")
Run Code Online (Sandbox Code Playgroud)
不起作用:
router := gin.Default()
router.GET("/ping", pong)
router.Use(cors.Default())
router.Run(":8082")
Run Code Online (Sandbox Code Playgroud)
有一个包https://github.com/rs/cors,可以以正确的方式处理 CORS 请求。它具有流行路由器的示例,包括gin
. 那它:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
cors "github.com/rs/cors/wrapper/gin"
)
func main() {
router := gin.Default()
router.Use(cors.Default())
router.GET("/", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{"hello": "world"})
})
router.Run(":8080")
}
Run Code Online (Sandbox Code Playgroud)
在常见情况下,您只需将默认处理添加router.Use(cors.Default())
到gin
. 这就足够了。
还有官方的杜松子酒的中间件来处理CORS请求github.com/gin-contrib/cors。
您可以使用安装它$ go get github.com/gin-contrib/cors
,然后像下面这样在您的应用程序中添加此中间件:package main
import (
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// CORS for https://foo.com and https://github.com origins, allowing:
// - PUT and PATCH methods
// - Origin header
// - Credentials share
// - Preflight requests cached for 12 hours
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"https://foo.com"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return origin == "https://github.com"
},
MaxAge: 12 * time.Hour,
}))
router.Run()
}
Run Code Online (Sandbox Code Playgroud)
小智 5
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用它
router = gin.New()
router.Use(CORSMiddleware())
Run Code Online (Sandbox Code Playgroud)