Golang中Cookie未解码的问题如何解决?
Golang中Cookie未解码的问题如何解决? 我正在尝试保存一个Cookie(这部分工作正常)并获取其值。
所使用的代码来自(网上有几个类似的链接,基本上都是相同的代码): https://git.schlagma.de/kulinari/kulinari-server/src/commit/096111466d81e4bfc91ccf1aab5723a3177308cf/cookie.go?lang=sv-EN
func getuser(r *http.Request) string {
var usr string
if cookie, err := r.Cookie("session"); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
usr = cookieValue["name"]
}
fmt.Println("---cookie.Value---")
fmt.Println(cookie.Value)
fmt.Println("---cookie.Value---")
fmt.Println("---cookieValue---")
fmt.Println(cookieValue)
fmt.Println("---cookieValue---")
fmt.Println("---usr---")
fmt.Println(usr)
fmt.Println("---usr---")
}
return usr
}
这段代码的结果是,我可以获取到加密后的值,但无法获取解码后的值。
—cookie.Value— (看起来没问题) TU5MjE0NTA2NXxkTnl3Mll6ZXZMeGhBeWVVYXRmMklFUHU4VFRXZWV3dGNyMktXMUt0ckE2SHAwM1M3MlJleUNDV2FGdWpFVW5SRUdmNjJ0N1MtTEV0cndPcW5BPT18AuIFYfSD96wKkbwWk7lTQLZgh8d1CHtsk1oHCNSI8vU= —cookie.Value—
—cookieValue—(映射是空的) map[] —cookieValue—
—usr— (没有用户名) —usr—
我哪里做错了?或者思路有问题?
更多关于Golang中Cookie未解码的问题如何解决?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
securecookie: 该值无效
如何使其有效?是编码失败了吗?
编辑:当我设置 cookie 时,我得到这个“编码后的”值:
session=MTU5MjE0OTUyNHx5QUFXdkxNbzQzbDBSWFl3SDRWclZlbjVTN3hYMlp1c3c3bFloaGNpZEpPR0pwNjQxVTRZcWtFUkd4c01Uc1RjdGY5OFpaMW90Q3JLTy04LUFBPT189LXZAtsR7Ghw_CvOsY6ZSAJ0dhD_hjtxqBrQNRS3Klw=; Path=/ <-------设置 cookie
当我获取 cookie 时,我得到相同的值:
—cookie.Value—
MTU5MjE0ODk5NHwzbmcxSkVyXzlzWFJ4Ukp5bWw4aC1jRGkxdVcwM2huNWdUYjNvb2swYVBQalZ0UWhvVzREdGJRWTBkUU5QZFc5UFItWUZERFVKVm9uMXJzYk1nPT188FTxxtXfFpepxtOPUeE4ifdNdqyMzRHPYp2avn0P7yY=
为什么我会收到这个错误?
securecookie: 该值无效
问题出在cookieHandler.Decode方法没有正确解码Cookie值。根据你的代码,cookieHandler应该是gorilla/securecookie的实例。以下是正确的实现方式:
import (
"github.com/gorilla/securecookie"
"net/http"
)
var cookieHandler = securecookie.New(
securecookie.GenerateRandomKey(64), // 哈希密钥
securecookie.GenerateRandomKey(32), // 块密钥
)
// 设置Cookie的示例
func setCookie(w http.ResponseWriter, username string) {
value := map[string]string{
"name": username,
}
if encoded, err := cookieHandler.Encode("session", value); err == nil {
cookie := &http.Cookie{
Name: "session",
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
}
// 获取Cookie的修正版本
func getuser(r *http.Request) string {
var usr string
if cookie, err := r.Cookie("session"); err == nil {
cookieValue := make(map[string]string)
// 关键:确保使用相同的密钥配置
if err = cookieHandler.Decode("session", cookie.Value, &cookieValue); err == nil {
usr = cookieValue["name"]
} else {
// 添加错误日志以便调试
fmt.Printf("解码错误: %v\n", err)
}
}
return usr
}
主要问题可能包括:
- 密钥不匹配:设置和读取Cookie时使用的密钥必须相同
- Cookie名称不一致:确保编码和解码时使用的cookie名称相同
检查你的cookieHandler初始化代码:
// 确保在整个应用中只初始化一次
var cookieHandler = securecookie.New(
[]byte("your-hash-key-here"), // 必须与设置Cookie时相同
[]byte("your-block-key-here"), // 必须与设置Cookie时相同
)
如果问题仍然存在,可以添加错误处理来诊断:
func getuser(r *http.Request) string {
if cookie, err := r.Cookie("session"); err == nil {
fmt.Printf("原始Cookie值: %s\n", cookie.Value)
cookieValue := make(map[string]string)
if err := cookieHandler.Decode("session", cookie.Value, &cookieValue); err != nil {
fmt.Printf("解码失败: %v\n", err)
return ""
}
fmt.Printf("解码后的值: %v\n", cookieValue)
return cookieValue["name"]
}
return ""
}

