问题描述:

    某次在使用docker部署服务时,通过https访问第三方服务时,提示错误x509: certificate signed by unknown authority,其中开发语言golang,docker系统ubuntu

解决方案:

1. client端调过tls校验或者使用http协议

package main

import (
	"crypto/tls"
	"fmt"
	"io/ioutil"
	"net/http"
)

func main()  {
	client := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				// InsecureSkipVerify 控制client端是否验证服务器的证书链和主机名,
				// 如果InsecureSkipVerify为true, crypto/tls接受服务器提供的任务和证书中的任何主机名
				// 在这种模式下,tls容易收到中间机器的攻击,除非是用自定义认证,建议只用于测试或者与VerifyConnection或VerifyPeerCertificate一起使用
				InsecureSkipVerify: true,
			},
		},
	}

	request, err := http.NewRequest("GET", "https://www.baidu.com", nil)
	if err != nil {
		panic(err)
	}
	res, err := client.Do(request)
	if err != nil {
		panic(err)
	}

	defer res.Body.Close()

	reply, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println("reply = ", string(reply))
}

2. Dockerfile中添加设置ca-certificates根证书

···
RUN apt-get update && apt-get install -y ca-certificates 
···
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐