英文:
Redirect to home page after saml authentification (login)
问题
我正在尝试使用crewjam库在Go语言的开源应用中集成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)
有人可以帮助我吗?
谢谢
英文:
I'm trying to integrate saml using crewjam library with an open-source app in go.
After authentication test using samltest.id, I want to be redirected to the home page.
I have tried several ways, but nothing works well, i'm using gorilla/mux router:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-go -->
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)
}
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
I have also tested :
<!-- language: lang-go -->
http.Redirect(w, r, "http://localhost:8080/", http.StatusFound)
<!-- end snippet -->
Can someone help me please?
Thanks
答案1
得分: 3
调用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
}
// 这一行被移除
// 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)
}
英文:
Calling w.Write
or writing into it using Fmt.Fprintf
requires HTTP status code to be set before, otherwise it sets default StatusOK
> // If WriteHeader is not called explicitly, the first call to Write
> // will trigger an implicit WriteHeader(http.StatusOK).
Setting the status code multiple times throws superfluous log.
Therefore, Your code is setting the HTTP status code to 200 (http.StatusOk)
, so the redirect after that is simply impossible.
Solution:
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)
}
答案2
得分: 2
尝试在写入内容之前发送你的标头。
并且可以使用相对位置
w.Header().Add("Location", "/")
w.WriteHeader(http.StatusFound)
fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())
英文:
Try to send your headers before writing content.
And optionally use a relative Location
w.Header().Add("Location", "/")
w.WriteHeader(http.StatusFound)
fmt.Fprintf(w, "Token contents, %+v!", sa.GetAttributes())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论