Http和golang
1 min read
1、HTTPS/TLS 服务
证书可以自签证书,也可以从CA购买证书,或者使用Let’s Encrypt的免费证书。
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
})
log.Fatal(http.ListenAndServeTLS(":8080", "./defaultCA.pem", "./defaultCA.key", nil))
}
1.1、创建自签名证书
#生成服务端私钥
openssl genrsa -out defaultCA.key 2048
#基于私钥生成自签名(x509)证书(.pem|.crt)
openssl req -x509 -new -nodes -key defaultCA.key -days 1024 -out defaultCA.pem
1.2、Let’s Encrypt
Let’s Encrypt是一个提供免费证书的非盈利机构,并且提供HTTP API来获取证书。
golang.org/x/crypto/acme/autocert包可以自动从Let’s Encrypt或者其他任何基于ACME CA获取证书。
Last updated on 2019-07-19