Go:通过证书验证经过身份验证的客户端的后续 http 请求
时间:2024-03-04
浏览:329
作者:佚名
目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《Go:通过证书验证经过身份验证的客户端的后续 http 请求》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~
问题内容
我目前正在编写一个 http 服务器(net/http),它托管多个端点,并在访问这些端点之前需要进行客户端身份验证(步骤 1)。身份验证成功后,服务器会发出一个短期令牌,客户端随后使用该令牌来访问这些端点。当客户端发送令牌(通过 http 标头)时,在每个处理程序函数的开头都有一段代码来检查客户端是否经过身份验证以及所提供的令牌是否有效。我正在寻找一个钩子/包装器,它可以拦截和验证客户端,而不是从每个端点函数调用 isauthenticated(r) 。
func getMyEndpoint(w http.ResponseWriter, r *http.Request) {
if valid := isAuthenticated(r); !valid {
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, "Invalid token or Client not authenticated."
return
}
...
}
func server() {
http.HandleFunc("/login", clientLoginWithCertAuth)
http.HandleFunc("/endpoint1", getMyEndpoint)
http.HandleFunc("/endpoint2", putMyEndpoint)
server := &http.Server{
Addr: ":443",
TLSConfig: &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.VerifyClientCertIfGiven,
},
}
if err := server.ListenAndServeTLS("cert.pem", "key.pem"); err != nil {
panic(err)
}
}解决方案
您可以创建一个可以包装 http.handlerfunc 的函数,例如像这样:
func handleauth(f http.handlerfunc) http.handlerfunc {
return func(w http.responsewriter, r *http.request) {
if valid := isauthenticated(r); !valid {
w.writeheader(http.statusunauthorized)
io.writestring(w, "invalid token or client not authenticated.")
return // this return is *very* important
}
// now call the actual handler, which is authenticated
f(w, r)
}
}
现在您还需要注册您的处理程序以通过将其包装在您的其他 http.handlerfuncs 上来使用它(仅那些明显需要身份验证的处理程序):
func server() {
// No authentication for /login
http.HandleFunc("/login", clientLoginWithCertAuth)
// Authentication required
http.HandleFunc("/endpoint1", handleAuth(getMyEndpoint))
http.HandleFunc("/endpoint2", handleAuth(putMyEndpoint))
server := &http.Server{
Addr: ":443",
TLSConfig: &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.VerifyClientCertIfGiven,
},
}
if err := server.ListenAndServeTLS("cert.pem", "key.pem"); err != nil {
panic(err)
}
}
这样,只有当 isauthenticated 针对该请求返回 true 时,您的处理程序才会被调用(由 handleauth),而无需在所有处理程序中复制代码。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
