我正在使用 GoLang 开发 API。对此 API 的所有调用都将包含一个 public_key
(用户名),有些还会在 Authorization header 中包含一个 private_key
(密码)。
我正在尝试弄清楚如何访问 Auth header 详细信息,以便我可以根据数据库检查凭据。
我正在使用 Julien Schmidt 的路由器和 Alice 来链接中间件。到目前为止我的设置是:
func main() {
session, err := mgo.Dial("conn-string")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := appContext{session.DB("db-name")}
commonHandlers := alice.New(context.ClearHandler)
router := NewRouter()
router.Get("/", commonHandlers.Append(basicAuthHandler).ThenFunc(c.mainHandler))
http.ListenAndServe(":5000", router)
}
但我不确定如何继续使用下面的 basicAuthHandler
函数。如果 public_key
存在,我需要检查它是否有效。 private_key
如果包含的话也是如此。
func basicAuthHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
}
return http.HandlerFunc(fn)
}
最佳答案
尝试运行函数 BasicAuth
根据要求:
user, password, ok := r.BasicAuth()
if !ok {
// could not get basic authentication credentials
}
关于go - 访问基本身份验证凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327588/