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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果某些權(quán)限檢查失敗,如何提前終止我的處理程序?

如果某些權(quán)限檢查失敗,如何提前終止我的處理程序?

Go
動(dòng)漫人物 2022-09-26 19:55:24
我正在尋找一種方法來實(shí)現(xiàn)權(quán)限檢查功能,使用http這個(gè)想法是有些API應(yīng)該只由登錄會(huì)話使用。func CheckPermissionFilter(w http.ResponseWriter, r *http.Response){    sid, err := r.Cookie("sid")    // check the permission with sid, if permission is granted then just let the     // process go on, otherwise, just break the filter chain and return Http Error Code.}func SomeHttpHandler(w http.ResponseWriter, r *http.Response){     CheckPermissionFilter(w, r)     // if not breaked by above filter function, process the request...   }我對(duì)權(quán)限檢查沒有問題,但我找不到破壞HTTP請(qǐng)求處理的方法。
查看完整描述

1 回答

?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

處理程序中的調(diào)用不能提前終止后者。相反,您應(yīng)該定義為中間件(另請(qǐng)參閱裝飾器模式):CheckPermissionFilterSomeHttpHandlerCheckPermissionFilter


package main


import (

    "net/http"

)


func main() {

    http.Handle("/foo", CheckPermissionFilter(SomeHttpHandler))

    // ...

}


func CheckPermissionFilter(h http.HandlerFunc) http.HandlerFunc {

    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

        sid, err := r.Cookie("sid")

        // handle err

        if !Validate(sid) {

            http.Error(w, "Unauthorized", http.StatusUnauthorized)

            return

        }

        h(w, r)

    })

}


func SomeHttpHandler(w http.ResponseWriter, r *http.Request) {

    // ...

}


func Validate(sid string) bool {

    return true // simplistic implementation for this example

}


查看完整回答
反對(duì) 回復(fù) 2022-09-26
  • 1 回答
  • 0 關(guān)注
  • 75 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)