Golang 安装SSL证书

一、登录会员中心,下载Nginx格式的SSL证书。

yourdomain.com.crt
yourdomain.com.key

二、Golang使用SSL证书 Demo

package main
import (
"crypto/tls"
"log"
"net/http"
)
func main() {
server := http.NewServeMux()
server.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello World!! "))
})
cfg := &tls.Config{
MinVersion:               tls.VersionTLS12,
CurvePreferences:         []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
srv := &http.Server{
Addr:         "127.0.0.1:443",
Handler:      server,
TLSConfig:    cfg,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
srv.ListenAndServeTLS("yourdomain.com.crt", "yourdomain.com.key")
}


 

相关新闻