亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

saml認證后重定向到首頁(登錄)

saml認證后重定向到首頁(登錄)

Go
眼眸繁星 2022-12-13 15:58:00
我正在嘗試使用crewjam庫將 saml 與開源應用程序集成在一起。使用 samltest.id 進行身份驗證測試后,我想被重定向到主頁。我嘗試了幾種方法,但效果不佳,我使用的是 gorilla/mux 路由器:func login(w http.ResponseWriter, r *http.Request) {    s := samlsp.SessionFromContext(r.Context())    if s == nil {        return    }    sa, ok := s.(samlsp.SessionWithAttributes)    if !ok {        return    }    fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())    w.Header().Add("Location", "http://localhost:8080/")    w.WriteHeader(http.StatusFound)}我也測試過:http.Redirect(w, r, "http://localhost:8080/", http.StatusFound)有人能幫助我嗎?
查看完整描述

2 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

使用調用w.Write或寫入它Fmt.Fprintf需要之前設置HTTP 狀態代碼,否則它設置默認值StatusOK

服務器.go

// 如果未顯式調用 WriteHeader,則第一次調用 Write
// 將觸發隱式 WriteHeader(http.StatusOK)。

多次設置狀態碼會拋出多余的日志。

因此,您的代碼將 HTTP 狀態代碼設置為200 (http.StatusOk),因此之后的重定向根本不可能。

解決方案:

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

    s := samlsp.SessionFromContext(r.Context())

    if s == nil {

        return

    }

    sa, ok := s.(samlsp.SessionWithAttributes)

    if !ok {

        return

    }

    // this line is removed 

    // fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())


    w.Header().Add("Location", "http://localhost:8080/")

    w.WriteHeader(http.StatusFound)

    // Or Simply 

    // http.Redirect(w, r, "http://localhost:8080/", http.StatusFound)

}


查看完整回答
反對 回復 2022-12-13
?
莫回無

TA貢獻1865條經驗 獲得超7個贊

嘗試在編寫內容之前發送標題。并可選擇使用相對位置


w.Header().Add("Location", "/")

w.WriteHeader(http.StatusFound)


fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())


查看完整回答
反對 回復 2022-12-13
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號