大猩猩会话未设置 cookie。

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

Gorilla Session not setting cookie

问题

这是我的Go服务器代码,我不知道为什么我的Gorilla Session不起作用。似乎一切都正常,直到session.save(r, w)。我已经使用Chrome开发工具检查了我的cookie,无论我做什么,都无法出现cookie。我知道我的身份验证已经有问题了,我只需要帮助让会话正常工作,这是我的目标。我不知道为什么这个函数不起作用,有人可以帮忙吗?

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/context"
	"github.com/gorilla/sessions"
)

var store = sessions.NewCookieStore([]byte("super-secret"))

func loginAuthHandler(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	username := r.FormValue("username")
	password := r.FormValue("password")
	fmt.Println("username:", username, "password:", password)

	if password == "welcome" && username == "guest" {
		fmt.Fprintf(w, "You logged in Succesfully!")

		session, _ := store.Get(r, "session")
		session.Values["authenticated"] = true
		session.Save(r, w)

		fmt.Println("session started!")
		fmt.Println(session)
	} else {
		fmt.Fprintf(w, "Wrong Login!")
	}
}

func secret(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "session")

	fmt.Println(session.Values["authenticated"])

	if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	fmt.Fprintf(w, "The cake is a lie!")

}

func main() {
	store.Options = &sessions.Options{
		Domain:   "localhost",
		Path:     "/",
		MaxAge:   3600 * 8,
		HttpOnly: true,
	}

	http.HandleFunc("/secret", secret)
	http.HandleFunc("/loginauth", loginAuthHandler)
	http.Handle("/", http.FileServer(http.Dir("public")))
	log.Fatal(http.ListenAndServe(":3002", context.ClearHandler(http.DefaultServeMux)))
}

这是我的index.html文件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Go Web App" />

    <link rel="stylesheet" href="index.css">

    <title>Login Form</title>
</head>

<body>
    <div class="container">
        <h1> Login Form </h1>
        <p> user: guest | pass: welcome</p> <br>

        <form action="/loginauth" method="POST">
            <label for="username">Name:</label><br>
            <input type="text" id="username" name="username"> <br>
            <label for="password">Password:</label> <br>
            <input type="password" id="password" name="password"> <br>
            <input type="submit" value="Submit">
        </form>
        
    </div>
    
</body>

</html>
英文:

here is the code for my go server, I have no idea why my gorilla session isn't working. it seems like everything works up to session.save(r, w). I already checked my cookies using the chrome dev tools and no matter what I do I can't get a cookie to appear. I know that my authentication is bad already I just need help with getting sessions working which is my goal. I don't know why this function isn't working can anybody help?

package main
import (
&quot;fmt&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;github.com/gorilla/context&quot;
&quot;github.com/gorilla/sessions&quot;
)
var store = sessions.NewCookieStore([]byte(&quot;super-secret&quot;))
func loginAuthHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
username := r.FormValue(&quot;username&quot;)
password := r.FormValue(&quot;password&quot;)
fmt.Println(&quot;username:&quot;, username, &quot;password:&quot;, password)
if password == &quot;welcome&quot; &amp;&amp; username == &quot;guest&quot; {
fmt.Fprintf(w, &quot;You logged in Succesfully!&quot;)
session, _ := store.Get(r, &quot;session&quot;)
session.Values[&quot;authenticated&quot;] = true
session.Save(r, w)
fmt.Println(&quot;session started!&quot;)
fmt.Println(session)
} else {
fmt.Fprintf(w, &quot;Wrong Login!&quot;)
}
}
func secret(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, &quot;session&quot;)
fmt.Println(session.Values[&quot;authenticated&quot;])
if auth, ok := session.Values[&quot;authenticated&quot;].(bool); !ok || !auth {
http.Error(w, &quot;Forbidden&quot;, http.StatusForbidden)
return
}
fmt.Fprintf(w, &quot;The cake is a lie!&quot;)
}
func main() {
store.Options = &amp;sessions.Options{
Domain:   &quot;localhost&quot;,
Path:     &quot;/&quot;,
MaxAge:   3600 * 8,
HttpOnly: true,
}
http.HandleFunc(&quot;/secret&quot;, secret)
http.HandleFunc(&quot;/loginauth&quot;, loginAuthHandler)
http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;public&quot;)))
log.Fatal(http.ListenAndServe(&quot;:3002&quot;, context.ClearHandler(http.DefaultServeMux)))
}

Here is my index.html file

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot; /&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;
&lt;meta name=&quot;description&quot; content=&quot;Go Web App&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;index.css&quot;&gt;
&lt;title&gt;Login Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;h1&gt; Login Form &lt;/h1&gt;
&lt;p&gt; user: guest | pass: welcome&lt;/p&gt; &lt;br&gt;
&lt;form action=&quot;/loginauth&quot; method=&quot;POST&quot;&gt;
&lt;label for=&quot;username&quot;&gt;Name:&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot;&gt; &lt;br&gt;
&lt;label for=&quot;password&quot;&gt;Password:&lt;/label&gt; &lt;br&gt;
&lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot;&gt; &lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 0

根据session.Save的文档:

Save是一个方便的方法,用于保存会话。它与调用store.Save(request, response, session)相同。在写入响应或从处理程序返回之前,应该调用Save

在你的代码中,在调用session.Save之前,你正在写入响应(fmt.Fprintf(w, "You logged in Succesfully!"))。这意味着在设置cookie之前,响应(包括包含cookie的标头)已经被写入(因此cookie不会发送到客户端)。

要解决这个问题,只需将fmt.Fprintf(w, "You logged in Succesfully!")移动到调用session.Save的下面即可。

英文:

As per the docs for session.Save

>Save is a convenience method to save this session. It is the same as calling store.Save(request, response, session). You should call Save before writing to the response or returning from the handler.

In your code you are writing to the response (fmt.Fprintf(w, &quot;You logged in Succesfully!&quot;)) before calling session.Save. This means that the response (including the headers that contain cookies) is written before the cookie gets set (so the cookies are not sent to the client).

To fix this just move fmt.Fprintf(w, &quot;You logged in Succesfully!&quot;) underneath the call to session.Save.

huangapple
  • 本文由 发表于 2022年8月7日 05:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73263370.html
匿名

发表评论

匿名网友

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

确定