从golang中传入的https请求中提取公共名称
我的api位于网关后面,网关终止来自客户端的ssl握手,并启动与我的api的单独握手。没有客户端应该直接调用我的api。我的要求是,我必须从传入的https请求中提取公共名称,并根据列表对其进行验证 我是新手,用了这个例子https://venilnoronha.io/a-step-by-step-guide-to-mtls-in-go 作为我使用https构建go服务器的起点 但不确定如何进一步从证书链的叶证书中提取公共名称从golang中传入的https请求中提取公共名称,go,tls1.2,mutual-authentication,Go,Tls1.2,Mutual Authentication,我的api位于网关后面,网关终止来自客户端的ssl握手,并启动与我的api的单独握手。没有客户端应该直接调用我的api。我的要求是,我必须从传入的https请求中提取公共名称,并根据列表对其进行验证 我是新手,用了这个例子https://venilnoronha.io/a-step-by-step-guide-to-mtls-in-go 作为我使用https构建go服务器的起点 但不确定如何进一步从证书链的叶证书中提取公共名称 package main import ( "crypto
package main
import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
// Write "Hello, world!" to the response body
io.WriteString(w, "Hello, world!\n")
}
func main() {
// Set up a /hello resource handler
http.HandleFunc("/hello", helloHandler)
// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("cert.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create the TLS Config with the CA pool and enable Client certificate validation
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()
// Create a Server instance to listen on port 8443 with the TLS config
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
}
// Listen to HTTPS connections with the server certificate and wait
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}
我应该能够打印证书链中的叶证书的通用名称。您可以从请求的TLS字段的VerifiedChains成员中检索它:
package main
import (
"crypto/tls"
"crypto/x509"
"io"
"io/ioutil"
"log"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
// Write "Hello, world!" to the response body
io.WriteString(w, "Hello, world!\n")
}
func main() {
// Set up a /hello resource handler
http.HandleFunc("/hello", helloHandler)
// Create a CA certificate pool and add cert.pem to it
caCert, err := ioutil.ReadFile("cert.pem")
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create the TLS Config with the CA pool and enable Client certificate validation
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()
// Create a Server instance to listen on port 8443 with the TLS config
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
}
// Listen to HTTPS connections with the server certificate and wait
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}
叶证书始终是链中的第一个。您可以从请求的TLS字段的VerifiedChains成员中检索它:
叶子证书始终是链中的第一个证书。谢谢@paul griffiths,它成功了。不幸的是,我无法更改您答案的状态,因为我的声望很低。@Ash:很高兴听到这个消息。如果答案对你有帮助,你可以通过点击复选标记来接受它,不管你的声誉如何。谢谢@paul griffiths,它成功了。不幸的是,我无法更改您答案的状态,因为我的声望很低。@Ash:很高兴听到这个消息。如果答案对您有帮助,您可以通过单击复选标记来接受它,而不管您的声誉如何。