在SAML身份验证(登录)后重定向到主页。

huangapple go评论82阅读模式
英文:

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)

有人可以帮助我吗?

谢谢 在SAML身份验证(登录)后重定向到主页。

英文:

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, &quot;Token contents, %+v!&quot;, sa.GetAttributes())

	w.Header().Add(&quot;Location&quot;, &quot;http://localhost:8080/&quot;)
	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, &quot;http://localhost:8080/&quot;, http.StatusFound)

<!-- end snippet -->

Can someone help me please?

Thanks 在SAML身份验证(登录)后重定向到主页。

答案1

得分: 3

调用w.Write或使用Fmt.Fprintf向其写入内容之前,需要先设置HTTP状态码,否则它会设置默认的StatusOK

Server.go

> 如果没有显式调用WriteHeader,第一次调用Write将触发隐式的WriteHeader(http.StatusOK)。

多次设置状态码会引发多余的日志。

因此,您的代码将HTTP状态码设置为200http.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

Server.go

> // 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, &quot;Token contents, %+v!&quot;, sa.GetAttributes())

    w.Header().Add(&quot;Location&quot;, &quot;http://localhost:8080/&quot;)
    w.WriteHeader(http.StatusFound)
    // Or Simply 
    // http.Redirect(w, r, &quot;http://localhost:8080/&quot;, 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(&quot;Location&quot;, &quot;/&quot;)
w.WriteHeader(http.StatusFound)

fmt.Fprintf(w, &quot;Token contents, %+v!&quot;, sa.GetAttributes())

huangapple
  • 本文由 发表于 2022年5月11日 22:59:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/72203308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定