TLS Config With Minimum Version
Pin a minimum TLS version, supply an explicit root pool, and set handshake timeouts so clients fail closed instead of negotiating weak protocols.
Code
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"time"
)
func main() {
// 信任池显式构造:生产环境应装入已知 CA,而不是留空
roots := x509.NewCertPool()
cfg := &tls.Config{
MinVersion: tls.VersionTLS12, // 拒绝 TLS 1.0/1.1
RootCAs: roots,
ServerName: "api.example.test",
}
transport := &http.Transport{
TLSClientConfig: cfg,
TLSHandshakeTimeout: 5 * time.Second,
ForceAttemptHTTP2: true,
}
client := &http.Client{
Transport: transport,
Timeout: 10 * time.Second,
}
fmt.Println("minimum version:", tls.VersionName(cfg.MinVersion))
fmt.Println("server name:", cfg.ServerName)
fmt.Println("client timeout:", client.Timeout)
fmt.Println("root pool empty:", roots.Equal(x509.NewCertPool()))
}How it works
Count the function main as the entire surface area of TLS Config With Minimum Version.
Using it
The demo output is labeled (minimum version:, server name:, client timeout:), so the printed lines read as a trace of the operations that ran.
Key statements
MinVersion: tls.VersionTLS12, // 拒绝 TLS 1.0/1.1
Interactive Tools for This Topic
CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.X.509 PEM Certificate DecoderDecode X.509 PEM certificates in pure JavaScript using ASN.1 DER parsing to inspect subject, issuer, SANs, key usage, and SHA-256 fingerprints.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.
FAQ
- What does TLS Config With Minimum Version print when run?
- Running the file directly is enough — expect labeled output lines such as `minimum version:`, `server name:`, `client timeout:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.