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

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

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

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

Go
動漫人物 2022-09-26 19:55:24
我正在尋找一種方法來實現(xiàn)權(quán)限檢查功能,使用http這個想法是有些API應(yīng)該只由登錄會話使用。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...   }我對權(quán)限檢查沒有問題,但我找不到破壞HTTP請求處理的方法。
查看完整描述

1 回答

?
慕虎7371278

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

處理程序中的調(diào)用不能提前終止后者。相反,您應(yī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

}


查看完整回答
反對 回復(fù) 2022-09-26
  • 1 回答
  • 0 關(guān)注
  • 72 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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