go 实现 https server POST 接口
参考链接:
-
TLS完全指南(一):TLS和安全通信 https://zhuanlan.zhihu.com/p/26684050
-
TLS完全指南(二):OpenSSL操作指南[实际操作失败] https://zhuanlan.zhihu.com/p/26684071
-
TLS完全指南(三):用Go语言写HTTPS程序 https://zhuanlan.zhihu.com/p/26684081
-
postman调用https接口方式: https://blog.csdn.net/jqc874789596/article/details/90437628
-
Golang 原生实现 HTTPS POST json 请求: https://blog.csdn.net/qq_32828933/article/details/107996039
1. 自签名证书生成
-
通过 OpenSSL 工具生成自签名证书:
# Generate CA private key openssl genrsa -out server.key 2048 # Generate CSR openssl req -new -key server.key -out server.csr # Generate Self Signed certificate(CA 根证书) openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
2. https server 实现
-
main.go
package main import ( "fmt" "io" "io/ioutil" "log" "net/http" ) func postHandler(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) if err != nil { log.Fatal("read request error") } fmt.Println("response Body:", string(body)) _, _ = io.WriteString(w, "Success") } func main() { http.HandleFunc("/post", postHandler) err := http.ListenAndServeTLS(":4321", "./crt_key/server.crt", "./crt_key/server.key", nil) if err != nil { log.Fatal("listen error:", err.Error()) } }
3. 测试
方式一:postman
1. 环境:
-
server:CentOS Linux release 7.8.2003 (Core)
-
client:postman
2. postman 设置 CA证书
-
关闭SSL
-
填写 server信息,加载CA证书
-
结果如下
3. 结果
-
client
-
server
方式二:go 编写 client