2015-04-02 16 views
9

Tôi đang sử dụng Go gin khuôn khổ ginGo khuôn khổ 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() 
     } 
    } 
} 

Tôi đã có Status Code: 200 OK, nhưng không có gì xảy ra sau khi yêu cầu OPTIONS. Có vẻ như tôi nhớ điều gì đó, nhưng tôi không thể hiểu tôi đang ở đâu sai.

Ai đó có thể giúp tôi không?

Trả lời

9

FWIW, đây là phần mềm trung gian CORS của tôi hoạt động theo nhu cầu của tôi.

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

Tại sao 204? Tại sao không 200? – qwertmax

+2

204 cho biết rằng không có nội dung. Nên làm việc theo một trong hai cách. – Caleb

0

Chúng tôi đã tạo một phần mềm trung gian tối thiểu.

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

và đăng ký nó với rượu gin middleware:

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

Ngoài ra còn có middleware chính thức gin của để xử lý yêu cầu CORS github.com/gin-contrib/cors.

Bạn có thể cài đặt nó bằng cách sử $ go get github.com/gin-contrib/cors và sau đó thêm middleware này trong ứng dụng của bạn như thế này: gói chính

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

Câu trả lời này được chuẩn bị bởi vì tôi vẫn có thể gặp phải câu hỏi này trong Google, vì vậy nó có thể khá hữu ích cho những người tìm kiếm giải pháp cho vấn đề gin-cors. Ngoài ra, văn bản của câu trả lời lặp lại readme từ trang github như liên kết thuần túy không phải là thực hành tốt cho câu trả lời SO. – Dracontis