2015-04-02 65 views
9

我使用轉到杜松子酒框架gin轉到杜松子酒框架CORS

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() 
     } 
    } 
} 

我有狀態碼:200行,但之後OPTIONS請求沒有任何反應。 它看起來像我想念的東西,但我不明白我在哪裏錯了。

任何人都可以幫助我嗎?

回答

9

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() 
    } 
} 
+0

爲什麼204?爲什麼不是200? – qwertmax 2015-04-04 06:38:16

+2

204表示沒有身體。應該以任何方式工作。 – Caleb 2015-04-04 19:27:25

0

我們創建了一個最小的中間件。

import (
    "github.com/gin-gonic/gin" 
    "net/http" 
) 

type optionsMiddleware struct { 

} 

func CreateOptionsMiddleware() *optionsMiddleware{ 
    return &optionsMiddleware{} 
} 

func (middleware *optionsMiddleware)Response(context *gin.Context){ 
    if context.Request.Method == "OPTIONS" { 
     context.AbortWithStatus(http.StatusNoContent) 
    } 
} 

與杜松子酒中間件進行註冊:

app := gin.New() 
app.Use(middleware.CreateOptionsMiddleware().Response). 
    Use(next-middleware)...... 
0

還有官方杜松子酒的處理CORS請求github.com/gin-contrib/cors中間件。

你可以使用$ go get github.com/gin-contrib/cors安裝它,然後在你的應用程序像這樣添加此中間件: 包主要

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() 
} 
+0

這個答案是準備好的,因爲我仍然可以在Google中遇到這個問題,所以對於尋求解決金酒問題的人來說,它可能非常有用。此外,答案的文字重複從github頁面自述,因爲純粹的鏈接是不好的做法,以回答。 – Dracontis 2018-02-13 09:35:22