php小編草莓為您介紹CORS gRPC 網關 GoLang。CORS是跨源資源共享的縮寫,是一種用于在瀏覽器中進行跨域請求的機制。gRPC是一種高性能、開源的遠程過程調用(RPC)框架,可用于構建分布式系統。而GoLang是一種強大的編程語言,具有高并發和簡潔的特點。CORS gRPC 網關 GoLang是將CORS和gRPC結合起來,通過Go語言實現網關,實現跨域請求的功能。在本文中,我們將詳細介紹CORS gRPC 網關 GoLang的原理和使用方法,幫助您更好地理解和應用該技術。
問題內容
我有一個 vue.js 3 前端,我通過 grpc-gateway
調用 golang 后端。我已經這樣做了一段時間,但我看到了隧道盡頭的曙光。
我目前面臨 cors 問題。然而,我正在閱讀有關如何處理它的相互矛盾的信息。因此,我想發帖,希望對大家有所幫助。
這里是我如何為 grpc(網關)初始化 mux 服務器的代碼
func runhttpserver(server *http.server, httpendpoint, grpcendpoint, swaggerpath string) (err error) { server.addr = httpendpoint ctx, cancel := context.withcancel(context.background()) defer cancel() // register groc server endpoint mux := runtime.newservemux( runtime.witherrorhandler(func(ctx context.context, mux *runtime.servemux, marshaler runtime.marshaler, w http.responsewriter, r *http.request, err error, ) { s, ok := status.fromerror(err) if ok { if s.code() == codes.unavailable { err = status.error(codes.unavailable, errunavailable) } } runtime.defaulthttperrorhandler(ctx, mux, marshaler, w, r, err) }), ) opts := []grpc.dialoption{ grpc.withtransportcredentials(insecure.newcredentials()), grpc.withchainunaryinterceptor(), } if err = api.registerapiservicehandlerfromendpoint(ctx, mux, grpcendpoint, opts); err != nil { return } swmux := http.newservemux() swmux.handle("/", mux) serveswagger(swmux, swaggerpath) server.handler = swmux return server.listenandserve() }
登錄后復制
這里是我認為應該添加 cors 配置的地方,但我不確定這就是我在 server.go 文件中設置它的方式..
var httpserver http.server // run http server with grpc gateway g.go(func() error { fmt.println("starting http sever (port {}) and grpc gateway (port {})", strconv.itoa(cfg.server.httpport), strconv.itoa(cfg.server.grpcport), ) return rest.runhttpserver( &httpserver, ":"+strconv.itoa(cfg.server.httpport), ":"+strconv.itoa(cfg.server.grpcport), "/webapi", ) })
登錄后復制
控制臺錯誤:
access to xmlhttprequest at 'http://localhost:8080/v1/test' from origin 'http://localhost:9000' has been blocked by cors policy: response to preflight request doesn't pass access control check: no 'access-control-allow-origin'
登錄后復制
我不知道在哪里添加類似的內容
func enablecors(w *http.responsewriter) { (*w).header().set("access-control-allow-origin", "*") }
登錄后復制
我覺得 golang grpc 網關應該內置一些東西,但我找不到任何東西?
如有任何建議,我們將不勝感激。
—–更新1—–
我已經嘗試過
func enablecors(h http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { w.header().set("access-control-allow-origin", "http://localhost:9000") w.header().set("access-control-allow-methods", "get, put, post, delete, head, options") h.servehttp(w, r) }) }
登錄后復制
和
func enablecors(h http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { w.header().set("access-control-allow-origin", "*") w.header().set("access-control-allow-methods", "get, put, post, delete, head, options") h.servehttp(w, r) }) }
登錄后復制
和
func enablecors(h http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { w.header().set("access-control-allow-origin", "http://localhost") w.header().set("access-control-allow-methods", "get, put, post, delete, head, options") h.servehttp(w, r) }) }
登錄后復制
結合
func serveSwagger(mux *http.ServeMux, swaggerPath string) { fileServer := http.FileServer(http.Dir(swaggerPath)) prefix := "/swagger-ui" mux.Handle(prefix, http.StripPrefix(prefix, fileServer)) }
登錄后復制
仍然有同樣的問題..非常令人沮喪
解決方法
根據您在評論中提供的最新錯誤:
從源“localhost:9000”訪問“localhost:8080/v1/test”處的 xmlhttprequest 已被 cors 策略阻止:對預檢請求的響應未通過訪問控制檢查:它沒有 http 正常狀態。
您的瀏覽器正在發送預檢請求(options
http方法)以確定是否可以發出所需的跨源請求。
服務器正在響應非 2xx 響應。
我懷疑這是因為您的 enablecors
函數正在將請求傳播到 grpc-gateway 處理程序,該處理程序對 options
http 方法不滿意并返回錯誤狀態,可能是:
< http/1.1 501 not implemented < content-type: application/json < vary: origin < date: fri, 25 nov 2022 11:17:52 gmt < content-length: 55 < {"code":12,"message":"method not allowed","details":[]}
登錄后復制
因此,為了避免這種情況,您希望在發出預檢請求時不進一步傳播請求,例如
func enablecors(h http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { w.header().set("access-control-allow-origin", "http://localhost:9000") w.header().set("access-control-allow-methods", "get, put, post, delete, head, options") if r.method == http.methodoptions { w.writeheader(http.statusnocontent) return } h.servehttp(w, r) }) }
登錄后復制
但是,上述內容可能仍然不是 cors 處理的合理實現。您應該為此使用現有的包,例如github.com/rs/cors,它將以合理的方式處理這個問題,并處理任何潛在的陷阱等。
因此導入 github.com/rs/cors
然后執行以下操作:
server.Handler = cors.AllowAll().Handler(swMux)
登錄后復制
應該讓你開始讓一切通過。該庫將允許您根據需要定制特定的來源、http 方法等。