第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于http中間件訪問數(shù)據(jù)

基于http中間件訪問數(shù)據(jù)

Go
長風秋雁 2022-07-11 14:57:24
使用這個https://github.com/praveen001/go-passport帶有 passsport 中間件的路徑:app.Put("/api/auth/login", p.Authenticate("local", MyHandler))護照認證功能:// Authenticate calls `Strategy.Authenticate` method of registered strategies, and checks the `passport.Result` returned by it.//// The result is stored in the request context with `passport.CtxKey` as key.//func (p *Passport) Authenticate(name string, h http.HandlerFunc) http.HandlerFunc {    return func(w http.ResponseWriter, r *http.Request) {        s, ok := p.Options.strategies[name]        if !ok {            w.WriteHeader(404)            return        }        s.Authenticate(w, r, func(res *Result) {            res.StrategyName = name                        ctx := context.WithValue(r.Context(), CtxKey, res)            h.ServeHTTP(w, r.WithContext(ctx))        })    }}因此,這會附加來自我的 auth 方法的響應并返回一個 http.HandleFunc,我可以自己定義(MyHandler)。func MyHandler(w http.ResponseWriter, r *http.Request) {    // Where is the data attached from the Authenticate func?    w.Header().Add("Content-Type", "application/json")    w.WriteHeader(401)    io.WriteString(w, `{"status":"ok"}`)}我不明白在哪里可以從Authenticatefunc 獲取附加數(shù)據(jù)。評論說:“//結果存儲在請求上下文中,passport.CtxKey作為鍵”。我在r *http.Request.
查看完整描述

1 回答

?
30秒到達戰(zhàn)場

TA貢獻1828條經(jīng)驗 獲得超6個贊

http.Request對象有一個Context()為您提供上下文的方法。您可以通過它訪問密鑰。

例如

ctx := r.Context()
value := ctx.Value(passport.CtxKey)


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號