net::ERR_CERT_INVALID 使用 Go 后端

net::ERR_CERT_INVALID using Go backend

我目前在 :443 上有一个 API 运行,如下所示:

// RunAsRESTAPI runs the API as REST API
func (api *API) RunAsRESTAPI(restAddr string) error {

    // Generate a `Certificate` struct
    cert, err := tls.LoadX509KeyPair( ".certificates/my-domain.crt", ".certificates/my-domain.key" )
    if err != nil {
        return errors.New(fmt.Sprintf("couldn't load the X509 certificates: %v\n", err))
    }

    // create a custom server with `TLSConfig`
    restAPI := &http.Server{
        Addr: restAddr,
        Handler: nil, // use `http.DefaultServeMux`
        TLSConfig: &tls.Config{
            Certificates: []tls.Certificate{ cert },
        },
    }

    // Defining the routes
    routes := map[string]func(http.ResponseWriter, *http.Request){
        "": api.handleIndex,
    }

    // Initialize mux
    mux := http.NewServeMux()

    // Register endpoints handlers
    for route, function := range routes {
        endpoint := "/" + route
        mux.HandleFunc(endpoint, function)
        log.Printf("[%s] endpoint registered.\n", endpoint)
    }

    // cors.Default() setup the middleware with default options being
    // all origins accepted with simple methods (GET, POST). See
    // documentation below for more options.
    restAPI.Handler = cors.Default().Handler(mux)

    log.Printf("REST TLS Listening on %s\n", restAddr)
    return restAPI.ListenAndServeTLS("", "")
}

我是这样创建证书的:

$ openssl req -new -newkey rsa:2048 -nodes -keyout my-domain.key -out my-domain.csr
$ openssl x509 -req -days 365 -in my-domain.csr -signkey my-domain.key -out my-domain.crt

然后我进行了 docker 化,然后部署到 Google Compute Engine,但是,我在从 ReactJs 应用程序请求我的 API 时仍然得到这个 net::ERR_CERT_INVALID(Google Chrome)

我在 Postman 上没有问题..我不明白,它甚至说 this certificate has not been verified by a third party

我有点迷茫,老实说,我该如何解决这个问题?所以我的应用程序可以请求我的 HTTPS 后端

谢谢

这一行应该有错误检查:

cert, err := tls.LoadX509KeyPair( ".certificates/my-domain.crt", ".certificates/my-domain.key" )
if err != nil {
    log.Fatalf("key-pair error: %v", err)
}

虽然它可能在本地工作,但您不能确定它在您的 Docker 容器中也能工作。

那些 key/cert 文件可用吗?如果不是 - 并且没有错误检查 - 你实际上将 nil 证书传递给你的 TLS 配置,这将没有任何效果,也没有其他可识别的错误。

您使用的是自签名证书,因此您的浏览器不信任它 -- 设计使然。邮递员也通过该消息告诉您这一点。这不是一个 go 问题,它正按照您的指示进行操作:使用不安全的证书为您的应用程序提供服务。

选项

  1. 不使用自签名证书,而是使用 LetsEncrypt 生成一个被认为有效的免费证书,因为它有支持的证书颁发机构。

  2. 如果您的证书对于您的用途足够安全,请将其作为根 CA 添加到您的客户端(浏览器)。如果不这样做,其他人将无法连接到 API。

感谢您的回答。我实际上找到了一种让它以超级简单的方式工作的方法:

func (api *API) RunAsRESTAPI() error {

    // Defining the routes
    routes := map[string]func(http.ResponseWriter, *http.Request){
        "": api.handleIndex,
    }

    // Initialize mux
    mux := http.NewServeMux()

    // Register endpoints handlers
    for route, function := range routes {
        endpoint := "/" + route
        mux.HandleFunc(endpoint, function)
        log.Printf("[%s] endpoint registered.\n", endpoint)
    }

    // cors.Default() setup the middleware with default options being
    // all origins accepted with simple methods (GET, POST). See
    // documentation below for more options.
    handler := cors.Default().Handler(mux)

    log.Printf("REST TLS Listening on %s\n", "api.my-domain.com")
    return http.Serve(autocert.NewListener("api.my-domain.com"), handler)
}

autocert.NewListener("api.my-domain.com")直接解决了我的问题:)