2 回答

TA貢獻1856條經驗 獲得超11個贊
使用調用w.Write
或寫入它Fmt.Fprintf
需要之前設置HTTP 狀態代碼,否則它設置默認值StatusOK
// 如果未顯式調用 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)
}

TA貢獻1865條經驗 獲得超7個贊
嘗試在編寫內容之前發送標題。并可選擇使用相對位置
w.Header().Add("Location", "/")
w.WriteHeader(http.StatusFound)
fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())
- 2 回答
- 0 關注
- 184 瀏覽
添加回答
舉報