登录
首页 >  Golang >  Go问答

GoLang CORS gRPC Gateway

来源:stackoverflow

时间:2024-02-05 22:54:22 359浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《GoLang CORS gRPC Gateway》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我有一个 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 方法等。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>