卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35440本站已运行396

CORS grpc 网关 GoLang

cors grpc 网关 golang

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 方法等。

卓越飞翔博客
上一篇: golang全局结构体初始化
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏