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 关注
- 144 浏览
添加回答
举报