Golang/gin:如何禁用HTTP重定向中的“Found.”链接

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

Golang/gin: How to disable the "Found." link on HTTP redirect

问题

我正在使用gin框架,当用户已经登录时,我将重定向登录页面到主页。主页上显示了以下HTML元素。

<a href="/signin">Found</a> "."

如何在应用程序中禁用该元素?

这是我的应用程序代码。

func GetSignIn(c *gin.Context) {
    // 从cookie中获取会话。检查电子邮件是否存在
    // 如果存在则重定向到主页,否则显示登录页面。
    session := sessions.Default(c)
    if session.Get("email") != nil {
        fmt.Println(session.Get("email"))
        c.Redirect(302, "/")
    }
    c.HTML(200, "signin.html", "")
}

看起来像是Go语言的一部分
https://github.com/golang/go/blob/master/src/net/http/server.go#L2014

但我不确定如何禁用它。如果有人能指导我就好了。

谢谢!

英文:

I'm using gin framework and I'm redirecting signin to home page when the user is already signed in. It shows this html element on the home page.

&lt;a href=&quot;/signin&quot;&gt;Found&lt;/a&gt; &quot;.&quot;

How to disable that element from the app?

This is my app code.

func GetSignIn(c *gin.Context) {
    // Get session from cookie. Check if email exists
    // redirect to Home page else show signin page.
    session := sessions.Default(c)
    if session.Get(&quot;email&quot;) != nil {
        fmt.Println(session.Get(&quot;email&quot;))
        c.Redirect(302, &quot;/&quot;)
    }
    c.HTML(200, &quot;signin.html&quot;, &quot;&quot;)
}

It seems like, it's a Go thing
https://github.com/golang/go/blob/master/src/net/http/server.go#L2014

But I'm not sure, how to disable it. Would be great if someone guide me.

Thanks!

答案1

得分: 1

是的,看起来这是一个Go语言的问题,你无法禁用它。
我建议你编写自己的Redirect结构,就像Gin中的render.Redirect一样(你可以将它嵌入到你的新结构中,以避免一些复制粘贴):

https://github.com/gin-gonic/gin/blob/master/render/redirect.go

为新结构编写自己的Render方法,基于net/http中的代码,然后像这样使用重定向:

https://github.com/gin-gonic/gin/blob/32cab500ecc71d2975f5699c8a65c6debb29cfbe/context.go#L476

我认为这是绕过net/http重定向行为的最简单方法。

英文:

Yeah, looks like it's a Go thing and you can't disable it.
I suggest you to write your own Redirect structure like render.Redirect from Gin (you can embed it in your new structure to avoid some copy pasting):

https://github.com/gin-gonic/gin/blob/master/render/redirect.go

Write your own Render method for new structure based on code from net/http and then use redirect like this:

https://github.com/gin-gonic/gin/blob/32cab500ecc71d2975f5699c8a65c6debb29cfbe/context.go#L476

I think it's the simplest way to bypass such net/http redirect behavior.

huangapple
  • 本文由 发表于 2017年6月4日 17:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/44352688.html
匿名

发表评论

匿名网友

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

确定