在让 MTLS 保护所有其他 HTTP 请求的同时,我可以有不安全的 GET HTTP 请求吗?

Can I have insecure GET HTTP requests whilst having MTLS securing all other HTTP requests?

我有一个用 golang 编写的 HTTP REST 服务,展示了我正在尝试的内容。

我希望 GET 请求不安全,而所有其他 REST 请求都使用 MTLS 保护。 我的实现已经使用了 gin web server library,所以如果可能我想坚持使用它。

我的问题是我只能将 tlsConfig 应用到两个组或两个都不应用。我一直无法找到在组级别应用此方法的方法。

package main

import (
    "crypto/tls"
    "crypto/x509"
    "errors"
    "fmt"
    "io/ioutil"
    "net/http"

    "log"

    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()

    // Unprotected public router for GET requests
    public := router.Group("/")

    // Private router with MTLS
    private := router.Group("/")

    public.GET("/insecure-ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "insecure pong",
        })
    })

    private.POST("/secure-ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "secure pong",
        })
    })

    // Get the SystemCertPool, continue with an empty pool on error
    rootCAs, err := x509.SystemCertPool()
    if err != nil {
        log.Fatal(err)
    }
    if rootCAs == nil {
        rootCAs = x509.NewCertPool()
    }

    // Create a CA certificate pool and add cacert.pem to it
    caCert, err := ioutil.ReadFile("cacert.pem")
    if err != nil {
        log.Fatal(err)
    }

    if ok := rootCAs.AppendCertsFromPEM(caCert); !ok {
        err := errors.New("failed to append CA cert to local system certificate pool")
        log.Fatal(err)
    }

    server := http.Server{
        Addr:    fmt.Sprintf(":%v", 8080),
        Handler: router,
    }

    server.TLSConfig = &tls.Config{
        RootCAs: rootCAs,
    }

    err = server.ListenAndServeTLS("certificate.crt", "privateKey.key")
    if err != nil {
        log.Fatal(err)
    }
}

只需创建两个 Server 实例并 运行 它们,一个 ListenAndServe 和一个 ListenAndServeTLS,配置相同的路由。因为 HTTP 和 HTTPS 在不同的端口上运行,所以它们必须有不同的侦听器,但是两个侦听器可以使用相同(或不同)的处理程序。例如:

publicRouter := gin.Default()

// Unprotected public router for GET requests
public := publicRouter.Group("/")

public.GET("/insecure-ping", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "insecure pong",
    })
})

server := http.Server{
    Addr:    fmt.Sprintf(":%v", 8081), // Or whatever
    Handler: publicRouter,
}

go func() {
    err = tlsServer.ListenAndServe()
    if err != nil {
        log.Fatal(err)
    }
}()

// Private router with MTLS
router := gin.Default()
private := router.Group("/")

private.POST("/secure-ping", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "secure pong",
    })
})

// Get the SystemCertPool, continue with an empty pool on error
rootCAs, err := x509.SystemCertPool()
if err != nil {
    log.Fatal(err)
}
if rootCAs == nil {
    rootCAs = x509.NewCertPool()
}

// Create a CA certificate pool and add cacert.pem to it
caCert, err := ioutil.ReadFile("cacert.pem")
if err != nil {
    log.Fatal(err)
}

if ok := rootCAs.AppendCertsFromPEM(caCert); !ok {
    err := errors.New("failed to append CA cert to local system certificate pool")
    log.Fatal(err)
}

tlsServer := http.Server{
    Addr:    fmt.Sprintf(":%v", 8080),
    Handler: router,
}

tlsServer.TLSConfig = &tls.Config{
    RootCAs: rootCAs,
}

err = tlsServer.ListenAndServeTLS("certificate.crt", "privateKey.key")
if err != nil {
    log.Fatal(err)
}